示例#1
0
def _print_last_login(values):
    if len(values) == 0:
        app_settings.error('last login message must be at least one line long')
        return
    rhs = []
    rhs_widths = []
    for value in values:
        if value.startswith('Log in:'):
            s = am.ansistring(
                f'<loginout>Log in:<loginout> <value>{value[len("Log in: "):]}</value>'
            )
        elif value.startswith('Log out:'):
            s = am.ansistring(
                f'<loginout>Log out:<loginout> <value>{value[len("Log out: "):]}</value>'
            )
        else:
            s = am.ansistring(f'<value>{value}</value>')
        rhs_widths.append(COLUMN_RH_WIDTH_1 + s.delta)
        rhs.append(s)
    lh = am.ansistring('<label>Last Login:</label>')
    lh_width = COLUMN_LH_WIDTH_1 + lh.delta
    for index, (rh, rh_width) in enumerate(zip(rhs, rhs_widths)):
        if index == 0:
            print(f'  {lh:{lh_width}} {rh:{rh_width}}')
        else:
            print(f'                  {rh:{rh_width}}')
示例#2
0
def github_backup_reposities(repositories, username, dest_dir_path):
    app_settings.info('backing up repositores...')
    for repository in repositories:
        app_settings.info(f'backing up github repository {repository.name} for {username}')
        if not github_backup_repository(repository, username, dest_dir_path):
            app_settings.error(f'quitting backup process due to error while backing up {repository.name}')
            return False
    return True
示例#3
0
def _get_user_repository_info(username, repository):
    app_settings.info(f'quering for user ({username}) repository ({repository}) information')
    uri = f'https://api.github.com/repos/{username}/{repository}'
    response_str = _call_uri(uri)
    if not response_str:
        app_settings.error(f'while trying to query {repository} under {username} account')
        return None
    repository_json = json.loads(response_str)
    return RepositoryInfo(repository_json)
def remove_invalid_subtitles(dir_name):
    for filename in _get_invalid_subtitle_filenames(dir_name):
        if app_settings.live_run:
            try:
                app_settings.info(f'{filename} live run -- deleting')
                os.remove(path)
                app_settings.info(f'{filename} live run -- deleted')
            except OSError as e:
                app_settings.error(f'{e.code} - {e.stderr} file: {filename}')
        else:
            app_settings.info(f'{filename} dry run -- doing nothing')
示例#5
0
def _call_uri(uri):
    try:
        app_settings.info(f'calling {uri} with a {_TIME_OUT} second timeout')
        response = requests.get(uri, timeout=_TIME_OUT)
        response.raise_for_status()
        response_str = response.text.strip()
        return None if not response_str else response_str
    except requests.exceptions.RequestException as e:
        app_settings.error(f'error while calling {uri}')
        app_settings.error(str(e))
        return None
def _get_subtitle_line_count(filename):
    args = shlex.split('wc -l "{}"'.format(filename))
    process = subprocess.Popen(args,
                               encoding='utf-8',
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    output, error = process.communicate()
    if process.wait() != 0:
        app_settings.error(f'{error} while getting line count on {filename}')
    match = re.match(r'^(?P<count>\d+)\s+.*$', output.strip())
    line_count = int(match.group('count')) if match else 0
    return line_count
示例#7
0
def _archive_and_compress(tar_path, backup_path):
    try:
        _remove_filesystem_object(tar_path)
        with tarfile.open(tar_path, 'w:bz2') as tar:
            tar.add(backup_path)
        app_settings.info(f'successfully created and populated bz2 tar file {tar_path}')
        return True
    except tarfile.TarError as err:
        app_settings.error(f'creating or populating {tar_path} with {backup_path}: {err}')
        return False
    except Exception as err:
        app_settings.error(f'creating or populating {tar_path} with {backup_path}: {err}')
        return False
示例#8
0
def _get_user_repository_infos(username):
    app_settings.info(f'quering for user ({username}) repositories information')
    uri = f'https://api.github.com/users/{username}/repos'
    response_str = _call_uri(uri)
    if not response_str:
        app_settings.error(f'while trying to query {repository} under {username} account')
        return None
    repositories_json = json.loads(response_str)
    repository_infos = []
    for repository_json in repositories_json:
        repository_infos.append(RepositoryInfo(repository_json))
        app_settings.info(f'found repository {repository_infos[-1].name} at {repository_infos[-1].git_url}')
    return repository_infos
示例#9
0
def _run_external_shell_command(cmd):
    try:
        completed_process = subprocess.run(cmd,
                                           shell=True,
                                           check=True,
                                           encoding='utf-8',
                                           capture_output=True)
        return completed_process.stdout
    except subprocess.SubprocessError as err:
        app_settings.error(f'"{cmd}" returned: {err}')
        return None
    except Exception as err:
        app_settings.error(f'"{cmd}" returned: {err}')
        return None
示例#10
0
def _run_external_command(cmd):
    args = shlex.split(cmd)
    try:
        completed_process = subprocess.run(args,
                                           check=True,
                                           encoding='utf-8',
                                           capture_output=True)
        return completed_process.stdout
    except subprocess.SubprocessError as err:
        if 'mailq' not in args:
            app_settings.error(f'"{cmd}" returned: {err}')
    except Exception as err:
        if 'mailq' not in args:
            app_settings.error(f'"{cmd}" returned: {err}')
    return None
示例#11
0
def _call_external_process(command, ignore_errors=False):
    args = shlex.split(command)
    try:
        app_settings.info(f'executing \'{command}\'')
        #subprocess.run(args, check=True, encoding='utf-8', shell=True, env=os.environ, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        subprocess.run(args, check=True, encoding='utf-8', stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        return True
    except subprocess.SubprocessError as err:
        if ignore_errors:
            app_settings.info(f'\'{command}\' returned: {err}')
            return True
        else:
            app_settings.error(f'\'{command}\' returned: {err}')
            return False
    except Exception as err:
        if ignore_errors:
            app_settings.info(f'\'{command}\' returned: {err}')
            return True
        else:
            app_settings.error(f'\'{command}\' returned: {err}')
            return False
示例#12
0
def main():
    _parse_args()
    try:
        repository_names = app_settings.repositories
        repository_infos = []
        if not repository_names:
            repository_infos = _get_user_repository_infos(app_settings.username)
        else:
            for repository_name in repository_names:
                repository_info = _get_user_repository_info(app_settings.username, repository_name)
                if repository_info:
                    repository_infos.append(repository_info)
        if repository_infos:
            dest_dir_path = Path(app_settings.destination).resolve()
            dest_dir_path.mkdir(parents=True, exist_ok=True)
            github_backup_reposities(repository_infos, app_settings.username, dest_dir_path)
        else:
            app_settings.error('there are no repositories to backup')
            return 1
    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr)
        return 1
    return 0
示例#13
0
def _output_environment_variable_values(environment_variables, raw, print_one_line, seperator):
    for environment_variable in environment_variables:
        if environment_variable in os.environ:
            value = os.environ[environment_variable]
            if raw:
                am.ansiprint(f'<name>{environment_variable}:</name> <text>{value}</text>')
            elif value.find(seperator) != -1 and not print_one_line:
                count = len(value.split(seperator))
                am.ansiprint(f'<name>{environment_variable} ({count}):<name>')
                output = ''
                for single_value in _get_information_on_values(value, seperator):
                    output += f'  <text>{single_value}</text>\n'
                am.ansiprint(output, end='')
            else:
                am.ansiprint(f'<name>{environment_variable}:</name> <text>{_get_information_on_value(value)}</text>')
        else:
            return app_settings.error(f'{environment_variable} is not defined within the current environment')
    return True
示例#14
0
def github_backup_repository(repository, username, dest_dir_path):
    timestamp = _get_backup_timestamp()
    if not github_backup_git_repository(repository , username, dest_dir_path, timestamp):
        app_settings.error(f'backing up github repository {repository.git_url}')
        return False
    if not github_backup_git_wiki(repository, username, dest_dir_path, timestamp):
        app_settings.error(f'backing up github wiki repository {repository.wiki_url}')
        return False
    if not github_backup_git_issues(repository, username, dest_dir_path, timestamp):
        app_settings.error(f'backing up github issues repository {repository.issues_url}')
        return False
    app_settings.info(f'successfully backed up {username} github repository {repository.git_url}')
    return True