Python sh.rm() Examples

The following are 28 code examples of sh.rm(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sh , or try the search function .
Example #1
Source File: device.py    From mobile-ai-bench with Apache License 2.0 6 votes vote down vote up
def pull(self, src_path, dst_path='.'):
        if self.system == SystemType.android:
            sh_commands.adb_pull(src_path, dst_path, self.address)
        elif self.system == SystemType.arm_linux:
            if os.path.isdir(dst_path):
                exist_file = dst_path + '/' + src_path.split('/')[-1]
                if os.path.exists(exist_file):
                    sh.rm('-rf', exist_file)
            elif os.path.exists(dst_path):
                sh.rm('-f', dst_path)
            try:
                sh.scp('-r',
                       '%s@%s:%s' % (self.username,
                                     self.address,
                                     src_path),
                       dst_path)
            except sh.ErrorReturnCode_1 as e:
                six.print_('Error msg {}'.format(e), file=sys.stderr)
                return 
Example #2
Source File: apt.py    From packman with Apache License 2.0 6 votes vote down vote up
def download(self, reqs, sources_path):
        """downloads component requirements

        :param list reqs: list of requirements to download
        :param sources_path: path to download requirements to
        """
        for req in reqs:
            # TODO: (TEST) add an is-package-installed check. if it is
            # TODO: (TEST) run apt-get install --reinstall instead of apt-get
            # TODO: (TEST) install.
            # TODO: (IMPRV) try http://askubuntu.com/questions/219828/getting-deb-package-dependencies-for-an-offline-ubuntu-computer-through-windows  # NOQA
            # TODO: (IMPRV) for downloading requirements
            lgr.debug('Downloading {0} to {1}...'.format(req, sources_path))
            o = sh.apt_get.install('-y', req, '-d', o='dir::cache={0}'.format(
                sources_path), _iter=True)
            for line in o:
                lgr.debug(line)
            sh.rm('{0}/*.bin'.format(sources_path)) 
Example #3
Source File: server_utils.py    From neural_chat with MIT License 5 votes vote down vote up
def delete_local_server(task_name):
    global server_process
    print('Terminating server')
    server_process.terminate()
    server_process.wait()
    print('Cleaning temp directory')

    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )
    sh.rm(shlex.split('-rf {}'.format(local_server_directory_path))) 
Example #4
Source File: package_app.py    From kivy-sdk-packager with MIT License 5 votes vote down vote up
def compile_app(appname):
    #check python Versions
    print('Compiling app...')
    py3 = appname + '/Contents/Frameworks/python/3.5.0/bin/python'
    pypath = appname + '/Contents/Resources'
    if exists(py3):
        print('python3 detected...')
        check_call(
            [pypath + '/script -OO -m compileall -b ' + pypath+'/myapp'],
            shell=True)
        print("Remove all __pycache__")
        check_call(
            ['/usr/bin/find -E {} -regex "(.*)\.py" -print0 |/usr/bin/grep -v __init__| /usr/bin/xargs -0 /bin/rm'.format(pypath+'/yourapp')],
             shell=True)
        check_call(
            ['/usr/bin/find -E {}/Contents/ -name "__pycache__" -print0 | /usr/bin/xargs -0 /bin/rm -rf'.format(appname)],
            shell=True)
    else:
        print('using system python...')
        check_call(
            [pypath + '/script -OO -m compileall ' + pypath+'/myapp'],
            shell=True)
        print("-- Remove all py/pyc/pyo")
        check_call(
            ['/usr/bin/find -E {} -regex "(.*)\.pyc" -print0 | /usr/bin/xargs -0 /bin/rm'.format(appname)],
            shell=True)
        check_call(
            ['/usr/bin/find -E {} -regex "(.*)\.pyo" -print0 | /usr/bin/xargs -0 /bin/rm'.format(appname)],
            shell=True)
        check_call(
            ['/usr/bin/find -E {} -regex "(.*)\.py" -print0 | /usr/bin/grep -v __init__ | /usr/bin/xargs -0 /bin/rm'.format(appname)],
            shell=True)
        print("-- Remove all .c")
        check_call(
            ['/usr/bin/find -E {} -regex "(.*)\.c" -print0 | /usr/bin/xargs -0 /bin/rm'.format(appname)],
            shell=True)
    sh.command('mv', pypath + '/myapp', pypath + '/yourapp') 
Example #5
Source File: package_app.py    From kivy-sdk-packager with MIT License 5 votes vote down vote up
def cleanup(appname, strip, gstreamer=True):
    if not strip:
        return
    print("stripping app")
    from subprocess import call
    call(["sh", "-x", "cleanup_app.sh" , "./"+appname])
    if gstreamer == 'no':
        sh.rm('-rf', '{}/Contents/Frameworks/GStreamer.framework'.format(appname))

    print("Stripping complete") 
Example #6
Source File: package_app.py    From kivy-sdk-packager with MIT License 5 votes vote down vote up
def bootstrap(source_app, appname):
    # remove mypackage.app if it already exists
    print('Copy Kivy.app/source.app if it exists')
    if exists(appname):
        print('{} already exists removing it...'.format(appname))
        sh.rm('-rf', appname)

    # check if Kivy.app exists and copy it
    if not exists(source_app):
        error("source app {} doesn't exist")
    print('copying {} to {}'.format(source_app, appname))
    sh.cp('-a', source_app, appname) 
Example #7
Source File: benchmarks.py    From enaml-native with MIT License 5 votes vote down vote up
def cleanup_app(config):
    if os.path.exists(config['app_dir']):
        sh.rm('-R', config['app_dir']) 
Example #8
Source File: test_cli.py    From enaml-native with MIT License 5 votes vote down vote up
def cleanup():
    if exists('tmp/test_cli/'):
        sh.rm('-R', 'tmp/test_cli/') 
Example #9
Source File: helpers.py    From rpl-attacks with GNU Affero General Public License v3.0 5 votes vote down vote up
def remove_folder(path):
    """
    This helper function is aimed to remove an entire folder. If the folder does not exist,
     it fails silently.

    :param path: absolute or relative source path
    """
    path = __expand_folders(path)
    try:
        sh.rm('-r', path)
    except sh.ErrorReturnCode_1:
        pass 
Example #10
Source File: helpers.py    From rpl-attacks with GNU Affero General Public License v3.0 5 votes vote down vote up
def remove_files(path, *files):
    """
    This helper function is aimed to remove specified files. If a file does not exist,
     it fails silently.

    :param path: absolute or relative source path
    :param files: filenames of files to be removed
    """
    path = __expand_folders(path)
    for file in files:
        try:
            sh.rm(join(path, file))
        except sh.ErrorReturnCode_1:
            pass 
Example #11
Source File: utils.py    From cstar_perf with Apache License 2.0 5 votes vote down vote up
def clean_directory(directory):
    """Remove all files in a directory"""

    for filename in os.listdir(directory):
        f = os.path.join(directory, filename)
        if os.path.isfile(f):
            sh.rm(f) 
Example #12
Source File: server_utils.py    From neural_chat with MIT License 5 votes vote down vote up
def delete_local_server(task_name):
    global server_process
    print('Terminating server')
    server_process.terminate()
    server_process.wait()
    print('Cleaning temp directory')

    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )
    sh.rm(shlex.split('-rf {}'.format(local_server_directory_path))) 
Example #13
Source File: server_utils.py    From neural_chat with MIT License 5 votes vote down vote up
def delete_local_server(task_name):
    global server_process
    print('Terminating server')
    server_process.terminate()
    server_process.wait()
    print('Cleaning temp directory')

    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )
    sh.rm(shlex.split('-rf {}'.format(local_server_directory_path))) 
Example #14
Source File: benchmark.py    From mobile-ai-bench with Apache License 2.0 5 votes vote down vote up
def run_on_device(target_device,
                  target_abi,
                  push_list,
                  executors,
                  target,
                  device_types,
                  host_bin_path,
                  bin_name,
                  input_dir,
                  benchmark_option,
                  benchmark_list,
                  result_files,
                  ):
    props = target_device.get_props()
    product_model = props["ro.product.model"]
    result_path = os.path.join(
        FLAGS.output_dir, product_model + "_" + target_device.target_soc
        + "_" + target_abi + "_" + "result.txt")
    sh.rm("-rf", result_path)
    device_bench_path = target_device.get_bench_path()
    target_device.exec_command("mkdir -p %s" % device_bench_path)
    bench_engine.push_all_models(target_device, device_bench_path, push_list)
    for executor in executors:
        avail_device_types = \
            target_device.get_available_device_types(device_types, target_abi,
                                                     executor)
        bench_engine.bazel_build(target, target_abi, executor,
                                 avail_device_types)
        bench_engine.bench_run(
            target_abi, target_device, host_bin_path, bin_name,
            benchmark_option, input_dir, FLAGS.run_interval,
            FLAGS.num_threads, FLAGS.max_time_per_lock,
            benchmark_list, executor, avail_device_types, device_bench_path,
            FLAGS.output_dir, result_path, product_model)
    result_files.append(result_path) 
Example #15
Source File: server_utils.py    From neural_chat with MIT License 5 votes vote down vote up
def delete_local_server(task_name):
    global server_process
    print('Terminating server')
    server_process.terminate()
    server_process.wait()
    print('Cleaning temp directory')
    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )
    sh.rm(shlex.split('-rf ' + local_server_directory_path)) 
Example #16
Source File: server_utils.py    From neural_chat with MIT License 5 votes vote down vote up
def setup_local_server(task_name):
    global server_process
    print("Local Server: Collecting files...")

    server_source_directory_path = os.path.join(
        parent_dir, server_source_directory_name
    )
    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )

    # Delete old server files
    sh.rm(shlex.split('-rf ' + local_server_directory_path))

    # Copy over a clean copy into the server directory
    shutil.copytree(server_source_directory_path, local_server_directory_path)

    print("Local: Starting server...")

    os.chdir(local_server_directory_path)

    packages_installed = subprocess.call(['npm', 'install'])
    if packages_installed != 0:
        raise Exception(
            'please make sure npm is installed, otherwise view '
            'the above error for more info.'
        )

    server_process = subprocess.Popen(['node', 'server.js'])

    time.sleep(1)
    print('Server running locally with pid {}.'.format(server_process.pid))
    host = input('Please enter the public server address, like https://hostname.com: ')
    port = input('Please enter the port given above, likely 3000: ')
    return '{}:{}'.format(host, port) 
Example #17
Source File: creator.py    From pyWinUSB with MIT License 5 votes vote down vote up
def close_stream(self):
        """ Zamykanie urządzenia i kasowanie plików tymczasowych
        """
        if self.source_mount is not None:
            sh.sync()
            sh.umount(self.source_mount, self.destination_mount)
            sh.rm(self.mount_folder, '-rf') 
Example #18
Source File: server_utils.py    From ParlAI with MIT License 5 votes vote down vote up
def delete_local_server(task_name):
    global server_process
    print('Terminating server')
    server_process.terminate()
    server_process.wait()
    print('Cleaning temp directory')

    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )
    sh.rm(shlex.split('-rf {}'.format(local_server_directory_path))) 
Example #19
Source File: server_utils.py    From ParlAI with MIT License 5 votes vote down vote up
def delete_local_server(task_name):
    global server_process
    print('Terminating server')
    server_process.terminate()
    server_process.wait()
    print('Cleaning temp directory')

    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )
    sh.rm(shlex.split('-rf {}'.format(local_server_directory_path))) 
Example #20
Source File: server_utils.py    From ParlAI with MIT License 5 votes vote down vote up
def delete_local_server(task_name):
    global server_process
    print('Terminating server')
    server_process.terminate()
    server_process.wait()
    print('Cleaning temp directory')

    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )
    sh.rm(shlex.split('-rf {}'.format(local_server_directory_path))) 
Example #21
Source File: server.py    From ParlAI with MIT License 5 votes vote down vote up
def delete_local_server(task_name):
    global server_process
    print('Terminating server')
    server_process.terminate()
    server_process.wait()
    print('Cleaning temp directory')
    local_server_directory_path = os.path.join(
        core_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )
    sh.rm(shlex.split('-rf ' + local_server_directory_path)) 
Example #22
Source File: server.py    From ParlAI with MIT License 5 votes vote down vote up
def setup_local_server(task_name):
    global server_process
    print("Local Server: Collecting files...")

    server_source_directory_path = os.path.join(core_dir, server_source_directory_name)
    local_server_directory_path = os.path.join(
        core_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )

    # Delete old server files
    sh.rm(shlex.split('-rf ' + local_server_directory_path))

    # Copy over a clean copy into the server directory
    shutil.copytree(server_source_directory_path, local_server_directory_path)

    print("Local: Starting server...")

    os.chdir(local_server_directory_path)

    packages_installed = subprocess.call(['npm', 'install'])
    if packages_installed != 0:
        raise Exception(
            'please make sure npm is installed, otherwise view '
            'the above error for more info.'
        )

    server_process = subprocess.Popen(['node', 'server.js'])

    time.sleep(1)
    print('Server running locally with pid {}.'.format(server_process.pid))
    host = input('Please enter the public server address, like https://hostname.com: ')
    port = input('Please enter the port given above, likely 3000: ')
    return '{}:{}'.format(host, port) 
Example #23
Source File: extract_data_from_xls.py    From census_explorer with MIT License 5 votes vote down vote up
def main():
    logger.info('Start to parse individual xls files')
    sh.rm('-rf', OUTPUT_PREFIX)
    sh.mkdir('-p', OUTPUT_PREFIX)
    files = [fn for fn in sh.ls(INPUT_PREFIX).split()]
    files = [fn for fn in files if fn.endswith('.xlsx')]

    # Extract xls to JSON
    pool = multiprocessing.Pool()
    pool.map(process_one_file, files)

    # Translation
    logger.info('Start to generate translation dicts')
    gen_translation(files) 
Example #24
Source File: ssh_device.py    From mobile-ai-bench with Apache License 2.0 5 votes vote down vote up
def pull(self, src_path, dst_path='.'):
        if os.path.isdir(dst_path):
            exist_file = dst_path + '/' + src_path.split('/')[-1]
            if os.path.exists(exist_file):
                sh.rm('-rf', exist_file)
        elif os.path.exists(dst_path):
            sh.rm('-f', dst_path)
        try:
            sh.scp('-r',
                   '%s@%s:%s' % (self.username, self.address, src_path),
                   dst_path)
        except sh.ErrorReturnCode_1 as e:
            six.print_('Error msg {}'.format(e), file=sys.stderr)
            return 
Example #25
Source File: server_utils.py    From neural_chat with MIT License 4 votes vote down vote up
def setup_local_server(task_name, task_files_to_copy=None):
    global server_process
    print("Local Server: Collecting files...")

    server_source_directory_path = os.path.join(
        parent_dir, legacy_server_source_directory_name
    )
    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )

    # Delete old server files
    sh.rm(shlex.split('-rf ' + local_server_directory_path))

    # Copy over a clean copy into the server directory
    shutil.copytree(server_source_directory_path, local_server_directory_path)

    # Consolidate task files
    task_directory_path = os.path.join(local_server_directory_path, task_directory_name)
    sh.mv(os.path.join(local_server_directory_path, 'html'), task_directory_path)

    hit_config_file_path = os.path.join(parent_dir, 'hit_config.json')
    sh.mv(hit_config_file_path, task_directory_path)

    for file_path in task_files_to_copy:
        try:
            shutil.copy2(file_path, task_directory_path)
        except IsADirectoryError:  # noqa: F821 we don't support python2
            dir_name = os.path.basename(os.path.normpath(file_path))
            shutil.copytree(file_path, os.path.join(task_directory_path, dir_name))
        except FileNotFoundError:  # noqa: F821 we don't support python2
            pass

    print("Local: Starting server...")

    os.chdir(local_server_directory_path)

    packages_installed = subprocess.call(['npm', 'install'])
    if packages_installed != 0:
        raise Exception(
            'please make sure npm is installed, otherwise view '
            'the above error for more info.'
        )

    server_process = subprocess.Popen(['node', 'server.js'])

    time.sleep(1)
    print('Server running locally with pid {}.'.format(server_process.pid))
    host = input('Please enter the public server address, like https://hostname.com: ')
    port = input('Please enter the port given above, likely 3000: ')
    return '{}:{}'.format(host, port) 
Example #26
Source File: server_utils.py    From ParlAI with MIT License 4 votes vote down vote up
def setup_local_server(task_name, task_files_to_copy=None):
    global server_process
    print("Local Server: Collecting files...")

    server_source_directory_path = os.path.join(
        parent_dir, legacy_server_source_directory_name
    )
    local_server_directory_path = os.path.join(
        parent_dir, '{}_{}'.format(local_server_directory_name, task_name)
    )

    # Delete old server files
    sh.rm(shlex.split('-rf ' + local_server_directory_path))

    # Copy over a clean copy into the server directory
    shutil.copytree(server_source_directory_path, local_server_directory_path)

    # Consolidate task files
    task_directory_path = os.path.join(local_server_directory_path, task_directory_name)
    sh.mv(os.path.join(local_server_directory_path, 'html'), task_directory_path)

    hit_config_file_path = os.path.join(parent_dir, 'hit_config.json')
    sh.mv(hit_config_file_path, task_directory_path)

    for file_path in task_files_to_copy:
        try:
            shutil.copy2(file_path, task_directory_path)
        except IsADirectoryError:  # noqa: F821 we don't support python2
            dir_name = os.path.basename(os.path.normpath(file_path))
            shutil.copytree(file_path, os.path.join(task_directory_path, dir_name))
        except FileNotFoundError:  # noqa: F821 we don't support python2
            pass

    print("Local: Starting server...")

    os.chdir(local_server_directory_path)

    packages_installed = subprocess.call(['npm', 'install'])
    if packages_installed != 0:
        raise Exception(
            'please make sure npm is installed, otherwise view '
            'the above error for more info.'
        )

    server_process = subprocess.Popen(['node', 'server.js'])

    time.sleep(1)
    print('Server running locally with pid {}.'.format(server_process.pid))
    host = input('Please enter the public server address, like https://hostname.com: ')
    port = input('Please enter the port given above, likely 3000: ')
    return '{}:{}'.format(host, port) 
Example #27
Source File: helpers.py    From rpl-attacks with GNU Affero General Public License v3.0 4 votes vote down vote up
def replace_in_file(path, replacements, logger=None):
    """
    This helper function performs a line replacement in the file located at 'path'.

    :param is_debug_flag: true if method is called from apply_debug_flag. Needed only for managing logger output
    :param path: path to the file to be altered
    :param replacements: list of string pairs formatted as [old_line_pattern, new_line_replacement]
    :param logger: logger object, optional. If not none, used to output if replacement is successful
    """
    tmp = path + '.tmp'
    if isinstance(replacements[0], string_types):
        replacements = [replacements]
    regexs = []
    for replacement in replacements:
        try:
            regex = re.compile(replacement[0])
        except re.error:
            regex = None
        regexs.append(regex)
    replacements_found = []
    with open(tmp, 'w+') as nf:
        with open(path) as of:
            for line in of.readlines():
                for replacement, regex in zip(replacements, regexs):
                    # try a simple string match
                    if replacement[0] in line:
                        line = line.replace(replacement[0], "" if replacement[1] in (None, '') else replacement[1])
                        replacements_found.append(replacement[0])
                    # then try a regex match
                    else:
                        if regex is not None:
                            match = regex.search(line)
                            if match is not None:
                                try:
                                    line = line.replace(match.groups(0)[0], "" if replacement[1] in (None, '') else replacement[1])
                                    replacements_found.append(match.groups(0)[0])
                                except IndexError:
                                    line = line.replace(match.group(), replacement[1])
                                    replacements_found.append([match.group()])
                                break
                # write changed line back
                nf.write(line)
    sh.rm(path)
    sh.mv(tmp, path)
    if logger:
        c = Counter(replacements_found)
        for k in c.keys():
            logger.debug("Found and replaced {} occurrence{}: \t{} ".format(c[k], ['', 's'][c[k] > 1], k))


# **************************************** JSON-RELATED HELPER ***************************************** 
Example #28
Source File: build.py    From declaracad with GNU General Public License v3.0 4 votes vote down vote up
def main(cfg):
    """ Build and run the app
    
    """
    try:
        #: Clean
        print("Clean...")
        sh.rm('-R', glob('build/*'))
    except:
        pass

    #: Build
    print("Build...")
    print(sh.python('release.py', 'build'))

    #: Enter build
    print("Trim...")
    with cd('build/exe.linux-x86_64-3.5/'):
        #: Trim out crap that's not needed
        for p in [
                #'libicu*',
                'lib/PyQt5/Qt',
                'lib/PyQt5/QtB*',
                'lib/PyQt5/QtDes*',
                'lib/PyQt5/QtH*',
                'lib/PyQt5/QtL*',
                'lib/PyQt5/QtM*',
                'lib/PyQt5/QtN*',
                'lib/PyQt5/QtPos*',
                'lib/PyQt5/QtT*',
                'lib/PyQt5/QtO*',
                'lib/PyQt5/QtSe*',
                'lib/PyQt5/QtSq*',
                'lib/PyQt5/QtQ*',
                'lib/PyQt5/QtWeb*',
                'lib/PyQt5/QtX*',
                #'platforms',
                #'imageformats',
                'libQt5Net*',
                'libQt5Pos*',
                'libQt5Q*',
                'libQt5Sq*',
                'libQt5O*',
                'libQt5T*',
                'libQt5Web*',
                #'declaracad',
                'libQt5X*'
                ]:
            try:
                sh.rm('-R', glob(p))
            except Exception as e:
                print(e)

        #: Test the app
        print("Launching...")
        cmd = sh.Command('./{name}'.format(**cfg))
        print(cmd())

    #: If good then build installer
    make_installer(cfg)