def update_repository(url, path): """Update repository at given path. If no checkout exists, do a new checkout from given url.""" revision = -1 if os.path.exists(path): SystemLogger.info("Updating repository") SystemLogger.info("Path: " + path) remote_url = info_remote_url(path) if url == remote_url: revision = info_local_revision_number(path) remote_revision = info_remote_revision_number(url) SystemLogger.debug("Local revision: " + str(revision)) SystemLogger.debug("Remote revision: " + str(remote_revision)) if revision == remote_revision: SystemLogger.success("Repository '" + path + "' is up-to-date at revision " + str(revision)) else: revision = update(path) SystemLogger.success("Repository '" + path + "' is updated to revision " + str(revision)) else: SystemLogger.error("Repository URL of existing directory '" + path + "' does not match expected remote url") SystemLogger.error("Existing URL: " + remote_url) SystemLogger.error("Expected URL: " + url) else: SystemLogger.info("Checking out repository") SystemLogger.info("URL: " + url) SystemLogger.info("Location: " + path) revision = checkout(url, path) SystemLogger.success("Checkout of revision " + str(revision) + " complete") return revision
def compress (path, basename=None): """ Compress the file or directory at path Argument path: Unicode string. Argument basename: Name of the archive to be generated. Unicode string, can be None. Result: Path to the compressed file. Unicode string. """ SystemLogger.debug("Compressing path") SystemLogger.debug("Path: " + path) result = None if basename == None: basename = os.path.basename(path) if platform.system() == 'Windows': output_file = os.path.join('.', basename + '.' + config.d_archive_extension) SystemLogger.debug("Destination: " + output_file) # TODO: get path from registry executable = os.path.join('C:\\', 'Program Files', '7-zip', '7z.exe') if os.path.isfile(executable): if execute([executable, 'a', output_file, path], SystemLogger.get_file()) == 0: SystemLogger.debug("Compression complete") result = output_file else: SystemLogger.error("Compression of '" + path + "' failed") else: SystemLogger.error("Comperssion of '" + path + "' failed. 7zip executable not found at " + executable) else: output_file = os.path.realpath(os.path.join('.', basename + '.' + config.d_archive_extension)) workingdir, compressdir = os.path.split(path) if execute(['tar', '-C', workingdir, '-cjf', output_file, compressdir], SystemLogger.get_file()) == 0: SystemLogger.debug("Compression complete") result = output_file else: SystemLogger.error("Compression of '" + path + "' failed") return result
def copy_files(src_glob_raw, destination_folder_raw): SystemLogger.info("copying files from " + src_glob_raw + " to " + destination_folder_raw) expanded_folder = os.path.expandvars (destination_folder_raw) for fname in glob.iglob(os.path.expandvars (src_glob_raw)): dst_file = os.path.join(expanded_folder, os.path.basename(fname)) SystemLogger.debug ("copying file from " + fname + " to " + dst_file) shutil.copy(fname, dst_file)
def execute_with_output (program, execution_directory = None): SystemLogger.debug ("Executing " + ' '.join (program)) if execution_directory is None: proc = subprocess.Popen(program, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in proc.stdout: SystemLogger.info (line.decode (v_encoding).rstrip()) proc.wait() SystemLogger.info("Finished with code " + str(proc.returncode)) return proc.returncode
def merge(path, url, rev_start, rev_end): """Merge repository at path with repository at url""" source_path = os.path.realpath(path) SystemLogger.debug("SVN merge") SystemLogger.debug("Path: " + source_path) SystemLogger.debug("URL: " + url) SystemLogger.debug("Revision from: " + str(rev_start)) SystemLogger.debug("Revision to: " + str(rev_end)) svn.merge_peg(url, rev_start, rev_end, rev_end, source_path)
def checkout(url, path): """Checkout HEAD revision from url to path""" global svn, svn_helper_revision, _has_pysvn SystemLogger.debug("SVN: checkout from " + url + " to " + path) if _has_pysvn: svn.checkout(url, path) revision = svn_helper_revision else: output = subprocess.check_output(['svn', 'checkout', url, path]).decode(v_encoding) expr = re.compile(r'.*Checked out revision\s([\d]+).*') revision = expr.search(output).group(1) SystemLogger.debug("SVN: revision " + str(revision)) return revision
def update(path, force=False): """Update repository at local path""" global svn, _has_pysvn SystemLogger.debug("SVN: updating repository at " + path) if _has_pysvn: if not is_up_to_date(path) or force: revision = svn.update(path)[0].number else: revision = info_local_revision_number(path) else: output = subprocess.check_output(['svn', 'update', path]).decode(v_encoding) expr = re.compile(r'.*revision\s([\d]+).*') revision = expr.search(output).group(1) SystemLogger.debug("SVN: revision " + str(revision)) return revision
def execute(program, output_file = None, execution_directory = None): SystemLogger.debug("Executing " + ' '.join(program)) if isinstance(output_file, str): pipe = open(output_file, 'a') else: pipe = output_file if execution_directory is None: proc = subprocess.Popen(program, stdin=pipe, stdout=pipe, stderr=pipe) else: proc = subprocess.Popen(program, cwd=execution_directory, stdin=pipe, stdout=pipe, stderr=pipe) proc.communicate() if isinstance(output_file, str): pipe.close() SystemLogger.debug("Finished with code " + str(proc.returncode)) return proc.returncode
def replace_in_file(path, search, replace): SystemLogger.debug("Replacing in file") SystemLogger.debug("Path: " + path) SystemLogger.debug("Search: " + search) SystemLogger.debug("Replace: " + replace) import fileinput for line in fileinput.FileInput(path, inplace=1): line = line.replace(search, replace) print (line),
def update_environment_variables(): # ISE_PLATFORM if not "ISE_PLATFORM" in os.environ or os.getenv("ISE_PLATFORM") != d_ise_platform: set_persistent_environment_variable("ISE_PLATFORM", d_ise_platform) # ISE_EIFFEL version, path = get_installed_version() if "ISE_EIFFEL" in os.environ and path == None: SystemLogger.debug ("WARNING: No nightly build available. Using ISE_EIFFEL = " + os.getenv("ISE_EIFFEL")) elif not "ISE_EIFFEL" in os.environ or os.getenv("ISE_EIFFEL") != path: set_persistent_environment_variable("ISE_EIFFEL", path) # else: # raise Exception ("Could not set ISE_EIFFEL environment variable.") # ISE_C_COMPILER if not "ISE_C_COMPILER" in os.environ or os.getenv("ISE_C_COMPILER") != d_ise_c_compiler: set_persistent_environment_variable("ISE_C_COMPILER", d_ise_c_compiler) # EIFFEL_SRC eiffel_source = os.path.realpath(elocation.trunk_source()) if not "EIFFEL_SRC" in os.environ or os.getenv("EIFFEL_SRC") != eiffel_source: set_persistent_environment_variable("EIFFEL_SRC", eiffel_source) # EWEASEL if not "EWEASEL" in os.environ or os.getenv("EWEASEL") != elocation.eweasel(): set_persistent_environment_variable("EWEASEL", elocation.eweasel()) # ISE_LIBRARY eiffel_source = os.path.realpath(elocation.trunk_source()) if not "ISE_LIBRARY" in os.environ or os.getenv("ISE_LIBRARY") != eiffel_source: set_persistent_environment_variable("ISE_LIBRARY", eiffel_source) # PATH: EiffelStudio # TODO: update PATH contents # Temporary path update for execution in same session os.environ['PATH'] = os.path.join(os.getenv("ISE_EIFFEL"), 'studio', 'spec', os.getenv("ISE_PLATFORM"), 'bin') + os.pathsep + os.environ['PATH'] if platform.system() != "Windows": with open (os.path.join (elocation.base_directory(), 'scripts', 'nightly.unix.sh'), 'w') as l_file: l_file.write ('export ISE_EIFFEL=') l_file.write (path) l_file.write ('\n') return
def download_file(url, filename): SystemLogger.debug("Downloading file") SystemLogger.debug("URL: " + url) SystemLogger.debug("Path: " + filename) try: response = urllib2.urlopen(url) data = response.read() path = os.path.realpath(filename) with open(path, "wb") as file: file.write(data) SystemLogger.debug("Download complete") except URLError as e: path = None if hasattr(e, 'reason'): SystemLogger.error("Download of '" + url + "' failed. Reason: " + e.reason) elif hasattr(e, 'code'): SystemLogger.error("Download of '" + url + "' failed with HTTP error code " + str(e.code)) else: raise e return path
def extract (a_file): """ Extract a_file to the current directory. Argument a_file: Unicode string. """ SystemLogger.debug("Extracting file") SystemLogger.debug("Path: " + a_file) if platform.system() == 'Windows': # TODO: get path from registry executable = os.path.join('C:\\', 'Program Files', '7-zip', '7z.exe') if os.path.isfile(executable): if execute([executable, 'x', a_file], SystemLogger.get_file()) == 0: SystemLogger.debug("Extraction complete") else: SystemLogger.error("Extraction of '" + a_file + "' failed") else: SystemLogger.error("Extraction of '" + a_file + "' failed. 7zip executable not found at " + executable) else: if execute(['tar', '-xjf', a_file], SystemLogger.get_file()) == 0: SystemLogger.debug("Extraction complete") else: SystemLogger.error("Extraction of '" + a_file + "' failed")
def commit(path, message): """Commit repository at path using the commit message""" SystemLogger.debug("SVN commit") SystemLogger.debug("Path: " + path) SystemLogger.debug("Message: " + message) svn.checkin(path, message)
def set_persistent_environment_variable(varname, value): SystemLogger.debug("setting environment variable " + varname + " to " + value) os.environ[varname] = value if platform.system() == 'Windows': execute(['setx', varname, value], SystemLogger.get_file()) return
def check_environment_variables(): result = True # ISE_PLATFORM if not "ISE_PLATFORM" in os.environ: SystemLogger.error("Environment variable ISE_PLATFORM not set") result = False elif os.getenv("ISE_PLATFORM") != d_ise_platform: SystemLogger.error("Value of environment variable ISE_PLATFORM (" + os.getenv("ISE_PLATFORM") + ") should be '" + d_ise_platform + "'") result = False else: SystemLogger.debug("ISE_PLATFORM = " + os.getenv("ISE_PLATFORM")) # ISE_EIFFEL if not "ISE_EIFFEL" in os.environ: SystemLogger.error("Environment variable ISE_EIFFEL not set") result = False elif not os.path.isdir(os.getenv("ISE_EIFFEL")): SystemLogger.error("Path from environment variable ISE_EIFFEL (" + os.getenv("ISE_EIFFEL") + ") does not exist") result = False elif not os.path.isdir(os.path.join(os.getenv("ISE_EIFFEL"), "studio", "spec", os.getenv("ISE_PLATFORM"))): SystemLogger.error("Installed EiffelStudio version invalid (no directory found under " + os.path.join(os.getenv("ISE_EIFFEL"), "studio", "spec", os.getenv("ISE_PLATFORM")) + ")") result = False else: SystemLogger.debug("ISE_EIFFEL = " + os.getenv("ISE_EIFFEL")) # ISE_C_COMPILER if not "ISE_C_COMPILER" in os.environ: SystemLogger.error("Environment variable ISE_C_COMPILER not set") result = False elif os.getenv("ISE_C_COMPILER") != d_ise_c_compiler: SystemLogger.error("Value of environment variable ISE_C_COMPILER (" + os.getenv("ISE_C_COMPILER") + ") should be '" + d_ise_c_compiler + "'") result = False else: SystemLogger.debug("ISE_C_COMPILER = " + os.getenv("ISE_C_COMPILER")) # EIFFEL_SRC if not "EIFFEL_SRC" in os.environ: SystemLogger.error("Environment variable EIFFEL_SRC not set. EVE compilation not possible") result = False elif not os.path.isdir(os.getenv("EIFFEL_SRC")): SystemLogger.error("Path from environment variable EIFFEL_SRC (" + os.getenv("EIFFEL_SRC") + ") does not exist") result = False else: SystemLogger.debug("EIFFEL_SRC = " + os.getenv("EIFFEL_SRC")) # EWEASEL if not "EWEASEL" in os.environ: SystemLogger.error("Environment variable EWEASEL not set. EVE compilation not possible") result = False elif not os.path.isdir(os.getenv("EWEASEL")): SystemLogger.error("Path from environment variable EWEASEL (" + os.getenv("EWEASEL") + ") does not exist") result = False else: SystemLogger.debug("EWEASEL = " + os.getenv("EWEASEL")) # ISE_LIBRARY if not "ISE_LIBRARY" in os.environ: SystemLogger.error("Environment variable ISE_LIBRARY not set. EVE compilation not possible") result = False elif not os.path.isdir(os.getenv("ISE_LIBRARY")): SystemLogger.error("Path from environment variable ISE_LIBRARY (" + os.getenv("ISE_LIBRARY") + ") does not exist") result = False else: SystemLogger.debug("ISE_LIBRARY = " + os.getenv("ISE_LIBRARY")) # PATH: EiffelStudio, Boogie, Z3 if not "PATH" in os.environ: SystemLogger.error("Environment variable PATH not set") result = False elif False: # TODO: check PATH contents result = False # final check if result: SystemLogger.success("Environment variables checked") return result