Skip to content

Commit

Permalink
switch to new version of upload script
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Meschke committed Oct 20, 2020
1 parent c003e6d commit c346272
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 17 deletions.
1 change: 1 addition & 0 deletions pubdate_dev.inf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2020-10-20 19:50:35
1 change: 1 addition & 0 deletions pubdate_live.inf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2020-10-20 19:47:07
78 changes: 63 additions & 15 deletions publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
FTP_PORT = 21
FTP_USER = 'f012a02e'
FTP_PASSWORD = 'pBk38S4v8Nbfbz35'
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"


class FtpSessionBase:
Expand Down Expand Up @@ -95,11 +96,13 @@ def buildSftpSession(self, host: str, port: int, username: str, password: str) -


def main() -> None:
environment = get_env_from_args()
args = get_args()
environment = get_env_from_args(args)
force_flag = get_force_flag_from_args(args)
print(f"=== Starting '{environment}' release ===")

update_pubdate_file()
file_list = collect_files_to_publish()
last_pubdate_timestamp = determine_last_pubdate_timestamp(environment, force_flag)
update_pubdate_file(environment)
file_list = collect_files_modified_after(last_pubdate_timestamp)

try:
ftpSession = FtpSessionBuilder().buildSession(FTP_HOST, FTP_PORT, FTP_USER, FTP_PASSWORD)
Expand All @@ -113,38 +116,83 @@ def main() -> None:
sys.exit()


def get_env_from_args() -> str:
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("environment", choices=[
'dev', 'live'], help="the environment the page should be published to, dev or live")
args = parser.parse_args()
parser.add_argument("-f", "--force", action="store_true", help="ignore the last publish datetime and push all files")
return parser.parse_args()


def get_env_from_args(args) -> str:
return args.environment


def update_pubdate_file() -> None:
pubdate_file = open("pubdate.inf", "w")
pubdate_file.write(time.strftime("%Y-%m-%d"))
def get_force_flag_from_args(args) -> bool:
return args.force


def determine_last_pubdate_timestamp(environment: str, force: bool) -> float:
if force:
print("Force flag was set, ignoring last pubdate")
return 0
else:
return get_last_pubdate_from_pubdate_file(environment)

def get_last_pubdate_from_pubdate_file(environment: str) -> float:
timestamp = 0
try:
pubdate_file = open(f"pubdate_{environment}.inf", "r")
time_string = pubdate_file.read()
pubdate_file.close()
time_struct = convert_time_string_to_struct_time(time_string)
timestamp = convert_struct_time_to_timestamp(time_struct)
except Exception:
print(f"Could not read from pubdate info file, all files will be published")
return timestamp


def convert_time_string_to_struct_time(time_string: str) -> time.struct_time:
return time.strptime(time_string, TIME_FORMAT)


def convert_struct_time_to_timestamp(time_struct: time.struct_time) -> float:
return time.mktime(time_struct)


def update_pubdate_file(environment: str) -> None:
pubdate_file = open(f"pubdate_{environment}.inf", "w")
pubdate_file.write(time.strftime(TIME_FORMAT))
pubdate_file.close()


def collect_files_to_publish() -> List[str]:
# expand the current folder into an absolute path
root_dir = os.path.abspath('.')
blacklist = ['.git', '.gitignore', '.idea', '.vscode', 'publish.py', 'README.md', '3rd-party']
def collect_files_modified_after(timestamp: float) -> List[str]:
root_dir = expand_current_folder_to_absolut_path()
blocklist = ['.git', '.gitignore', '.idea', '.vscode', 'publish.py', 'README.md']
files_to_publish = []
for subdir, _, files in os.walk(root_dir):
for file in files:
full_path = os.path.join(subdir, file)
ok = True
for item in blacklist:
for item in blocklist:
ok = ok and item not in full_path
ok = ok and file_changed_later_than(full_path, timestamp)
if ok:
full_path = full_path.replace(root_dir + "\\", '')
full_path = full_path.replace('\\', '/')
files_to_publish.append(full_path)
return files_to_publish


def expand_current_folder_to_absolut_path() -> str:
return os.path.abspath('.')


def file_changed_later_than(path: str, timestamp: float) -> bool:
last_modified_timestamp = os.path.getmtime(path)
return last_modified_timestamp > timestamp


def get_remote_base_dir_for(environment: str) -> str:
base_dir = FTP_LIVE_DIR
if environment == 'dev':
Expand All @@ -168,7 +216,7 @@ def publish_files_to(file_list: List[str], base_dir: str, ftp_session: FtpSessio
except ftplib.error_perm:
ftp_session.make_dir(next_segment)
ftp_session.change_dir(next_segment)
except Exception:
except Exception:
print(traceback.format_exc())
sys.exit()
ftp_session.put_file(file_name, path_segments.pop(0))
Expand Down
19 changes: 17 additions & 2 deletions scripts/displayLastPubDate.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
function loadLastPublishedDateInto(elementId){
var xhttp = new XMLHttpRequest();
let environment = determineCurrentEnvironment();
console.debug(environment);
writeLastPublishedDateForEnvIntoElement(environment, elementId);
}

function determineCurrentEnvironment(){
let currentUrl = window.location.href;
if(currentUrl.includes("dev.")){
return "dev";
}
return "live";
}

function writeLastPublishedDateForEnvIntoElement(environment, elementId){
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200){
displayLastPublishedDate(elementId, this.responseText);
}
};
xhttp.open("GET", "pubdate.inf", true);
xhttp.open("GET", "pubdate_" + environment + ".inf", true);
xhttp.send();

}

function displayLastPublishedDate(elementId, lastPublishedDate){
Expand Down

0 comments on commit c346272

Please sign in to comment.