Python os.makedirs() Examples

The following are code examples for showing how to use os.makedirs(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: factotum   Author: Denubis   File: update.py    GNU General Public License v3.0 6 votes vote down vote up
def copytree(src, dst, symlinks = False, ignore = None):
	if not os.path.exists(dst):
		os.makedirs(dst)
		shutil.copystat(src, dst)
	lst = os.listdir(src)
	if ignore:
		excl = ignore(src, lst)
		lst = [x for x in lst if x not in excl]
	for item in lst:
		s = os.path.join(src, item)
		d = os.path.join(dst, item)
		if symlinks and os.path.islink(s):
			if os.path.lexists(d):
				os.remove(d)
			os.symlink(os.readlink(s), d)
			try:
				st = os.lstat(s)
				mode = stat.S_IMODE(st.st_mode)
				os.lchmod(d, mode)
			except:
				pass # lchmod not available
		elif os.path.isdir(s):
			copytree(s, d, symlinks, ignore)
		else:
			shutil.copy2(s, d) 
Example 2
Project: fs_image   Author: facebookincubator   File: temp_subvolumes.py    MIT License 6 votes vote down vote up
def _prep_rel_path(self, rel_path: AnyStr):
        '''
        Ensures subvolumes live under our temporary directory, which
        improves safety, since its permissions ought to be u+rwx to avoid
        exposing setuid binaries inside the built subvolumes.
        '''
        rel_path = os.path.relpath(
            os.path.realpath(
                os.path.join(self._temp_dir, byteme(rel_path)),
            ),
            start=os.path.realpath(self._temp_dir),
        )
        if (
            rel_path == b'..' or rel_path.startswith(b'../') or
            os.path.isabs(rel_path)
        ):
            raise AssertionError(
                f'{rel_path} must be a subdirectory of {self._temp_dir}'
            )
        abs_path = os.path.join(self._temp_dir, rel_path)
        try:
            os.makedirs(os.path.dirname(abs_path))
        except FileExistsError:
            pass
        return abs_path 
Example 3
Project: incubator-spot   Author: apache   File: utils.py    Apache License 2.0 6 votes vote down vote up
def create_oa_folders(cls, type, date):

        # create date and ingest summary folder structure if they don't' exist.
        root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        data_type_folder = "{0}/data/{1}/{2}"
        if not os.path.isdir(data_type_folder.format(root_path, type, date)): os.makedirs(
            data_type_folder.format(root_path, type, date))
        if not os.path.isdir(data_type_folder.format(root_path, type, "ingest_summary")): os.makedirs(
            data_type_folder.format(root_path, type, "ingest_summary"))

        # create ipynb folders.
        ipynb_folder = "{0}/ipynb/{1}/{2}".format(root_path, type, date)
        if not os.path.isdir(ipynb_folder): os.makedirs(ipynb_folder)

        # retun path to folders.
        data_path = data_type_folder.format(root_path, type, date)
        ingest_path = data_type_folder.format(root_path, type, "ingest_summary")
        return data_path, ingest_path, ipynb_folder 
Example 4
Project: malcode   Author: moonsea   File: copyasm.py    GNU General Public License v3.0 6 votes vote down vote up
def CopyFile(srcDir, desDir, filename):
    if not os.path.exists(desDir):
        try:
            os.makedirs(desDir)
        except:
            print '[-] Mkdir error'

    desfile = os.path.join(desDir, filename)
    if os.path.isfile(desfile):
        log('Ignore', filename, '[-]', subpath='copyasm')
        print '[-]Ignore ', filename
        return
        # os.remove(desfile)

    log('Copying', filename, subpath='copyasm')
    print '[+]Copying ', filename

    try:
        shutil.copy(srcDir, desDir)
    except:
        log('Copying error', filename, '[-]', subpath='copyasm')
        print '[-]Copy error ', filename 
Example 5
Project: malcode   Author: moonsea   File: zeustracker.py    GNU General Public License v3.0 6 votes vote down vote up
def WriteRes(malName, malTitle, malDesc, malAuthor, malDate, malSrc):
    if not os.path.exists('malcode'):
        os.makedirs('malcode')

    malTitle = malTitle.replace('<em>', '').replace('</em>', '').replace('<strong>', '').replace('</strong>', '')
    malDesc = malDesc.replace('<em>', '').replace('</em>', '').replace('<strong>', '').replace('</strong>', '')
    malAuthor = malAuthor.replace('<em>', '').replace('</em>', '').replace('<strong>', '').replace('</strong>', '')
    malDate = malDate.replace('<em>', '').replace('</em>', '').replace('<strong>', '').replace('</strong>', '')

    content = '\n'.join([malName, malTitle, malDesc, malAuthor, malDate, malSrc])

    file_path = os.path.join('malcode', malName)

    with codecs.open(file_path, 'w', 'utf-8') as file:
        file.write(content)
    # file = codecs.open(file_path, 'a+', 'utf-8')
    # file.write(content + '\n')
    # file.close 
Example 6
Project: alfred-yubikey-otp   Author: robertoriv   File: web.py    MIT License 6 votes vote down vote up
def save_to_path(self, filepath):
        """Save retrieved data to file at ``filepath``.

        .. versionadded: 1.9.6

        :param filepath: Path to save retrieved data.

        """
        filepath = os.path.abspath(filepath)
        dirname = os.path.dirname(filepath)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        self.stream = True

        with open(filepath, 'wb') as fileobj:
            for data in self.iter_content():
                fileobj.write(data) 
Example 7
Project: flask-ci   Author: vicenteneto   File: ci.py    MIT License 6 votes vote down vote up
def __call__(self, *args, **kwargs):
        output_dir = kwargs[CI.OUTPUT_DIR]
        verbose = kwargs[CI.VERBOSE]

        if not os.path.exists(output_dir):
            if verbose:
                print('Creating output directory...')
            os.makedirs(output_dir)

        for task in self.tasks:
            if verbose:
                print('Executing {0}...'.format(task.__module__))
            task.run(self.settings, **kwargs)

        if verbose:
            print('Done') 
Example 8
Project: aospy   Author: spencerahill   File: calc.py    Apache License 2.0 6 votes vote down vote up
def _save_files(self, data, dtype_out_time):
        """Save the data to netcdf files in direc_out."""
        path = self.path_out[dtype_out_time]
        if not os.path.isdir(self.dir_out):
            os.makedirs(self.dir_out)
        if 'reg' in dtype_out_time:
            try:
                reg_data = xr.open_dataset(path)
            except (EOFError, RuntimeError, IOError):
                reg_data = xr.Dataset()
            reg_data.update(data)
            data_out = reg_data
        else:
            data_out = data
        if isinstance(data_out, xr.DataArray):
            data_out = xr.Dataset({self.name: data_out})
        data_out.to_netcdf(path, engine='netcdf4') 
Example 9
Project: claxon   Author: vanatteveldt   File: preprocess.py    GNU General Public License v3.0 6 votes vote down vote up
def handle(self, *args, **options):
        project = Project.objects.get(pk=options['project_id'])
        dir = os.path.join(settings.TOKEN_DIR, "project_{}".format(project.id))
        os.makedirs(dir, exist_ok=True)
        docids = set(project.document_set.values_list("id", flat=True))
        preprocessed = {int(x) for x in os.listdir(dir)}
        todo = list(docids - preprocessed)
        if not todo:
            print("All documents already preprocessed")
            return
        print("Preprocessing {} documents".format(len(todo)))
        m = get_model(project)
        chunks = [todo[i:i+_CHUNK_SIZE] for i in range(0, len(todo), _CHUNK_SIZE)]
        for chunk in tqdm(chunks):
            docs = list(Document.objects.filter(pk__in=chunk))
            texts = [doc.text for doc in docs]
            results = m.pipe(texts, disable="textcat")
            for doc, tokens in zip(docs, results):
                fn = os.path.join(dir, str(doc.id))
                tokens.to_disk(fn) 
Example 10
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: web.py    MIT License 6 votes vote down vote up
def save_to_path(self, filepath):
        """Save retrieved data to file at ``filepath``.

        .. versionadded: 1.9.6

        :param filepath: Path to save retrieved data.

        """
        filepath = os.path.abspath(filepath)
        dirname = os.path.dirname(filepath)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        self.stream = True

        with open(filepath, 'wb') as fileobj:
            for data in self.iter_content():
                fileobj.write(data) 
Example 11
Project: RelayBot2.0   Author: nukeop   File: logs.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, bot):
        super(Logs, self).__init__(bot)
        self.loggers = {}
        self.logs_path = os.path.join(os.getcwd(), "logs")
        self.friend_logs_path = os.path.join(self.logs_path, "friends")
        self.group_logs_path = os.path.join(self.logs_path, "groups")

        try:
            os.makedirs(self.friend_logs_path)
        except OSError:
            logger.error("Could not create {}".format(self.friend_logs_path))

        try:
            os.makedirs(self.group_logs_path)
        except OSError:
            logger.error("Could not create {}".format(self.group_logs_path)) 
Example 12
Project: CFN-CR-PythonLambdaLayer   Author: kisst   File: lambda_function.py    GNU General Public License v3.0 6 votes vote down vote up
def dump_text_to_file(filename, text, dirpath):
    """
    Save extra functions text into file within the layer.
    """
    # dump variable's contents to a file under dirpath
    abs_dirpath = os.path.abspath(f"{dirpath}/lambdalayer")
    try:
        os.makedirs(abs_dirpath)
    except FileExistsError:
        pass
    abs_initpath = os.path.abspath(os.path.join(abs_dirpath, '__init__.py'))
    with open(abs_initpath, mode='a'):
        os.utime(abs_initpath, None)
    abs_filepath = os.path.abspath(os.path.join(abs_dirpath, filename))
    with open(abs_filepath, mode='w', encoding='utf-8') as file_var:
        file_var.write(base64.b64decode(text).decode('utf-8')) 
Example 13
Project: wikilinks   Author: trovdimi   File: htmlstripper.py    MIT License 6 votes vote down vote up
def plaintext2zip(self, file_name, subdirname, plaintext):

        file_name=file_name.split('.')[0]
        plaintext_file_name = STATIC_PLAINTEXT_ARTICLES_DIR+subdirname+'/'+file_name+'.txt'
        zip_file_name = STATIC_PLAINTEXT_ARTICLES_DIR+subdirname+'/'+file_name+'.zip'

        if not os.path.exists(STATIC_PLAINTEXT_ARTICLES_DIR+subdirname):
            os.makedirs(STATIC_PLAINTEXT_ARTICLES_DIR+subdirname)


        with codecs.open(plaintext_file_name, 'w', encoding='utf8') as outfile:
            outfile.write(plaintext)
            outfile.flush()
            outfile.close()

        zf = zipfile.ZipFile(zip_file_name, mode='w', compression=zipfile.ZIP_DEFLATED)
        try:
            zf.write(plaintext_file_name, os.path.basename(plaintext_file_name))
            os.remove(plaintext_file_name)
        except Exception, e:
            print e
            logging.error('zip error %s ' % plaintext_file_name) 
Example 14
Project: Traffic_sign_detection_YOLO   Author: AmeyaWagh   File: build.py    MIT License 6 votes vote down vote up
def savepb(self):
		"""
		Create a standalone const graph def that 
		C++	can load and run.
		"""
		darknet_pb = self.to_darknet()
		flags_pb = self.FLAGS
		flags_pb.verbalise = False
		
		flags_pb.train = False
		# rebuild another tfnet. all const.
		tfnet_pb = TFNet(flags_pb, darknet_pb)		
		tfnet_pb.sess = tf.Session(graph = tfnet_pb.graph)
		# tfnet_pb.predict() # uncomment for unit testing
		name = 'built_graph/{}.pb'.format(self.meta['name'])
		os.makedirs(os.path.dirname(name), exist_ok=True)
		#Save dump of everything in meta
		with open('built_graph/{}.meta'.format(self.meta['name']), 'w') as fp:
			json.dump(self.meta, fp)
		self.say('Saving const graph def to {}'.format(name))
		graph_def = tfnet_pb.sess.graph_def
		tf.train.write_graph(graph_def,'./', name, False) 
Example 15
Project: fs_image   Author: facebookincubator   File: test_yum_dnf_from_snapshot.py    MIT License 5 votes vote down vote up
def _install(self, *, protected_paths, version_lock=None):
        install_root = Path(tempfile.mkdtemp())
        try:
            # IMAGE_ROOT/meta/ is always required since it's always protected
            for p in set(protected_paths) | {'meta/'}:
                if p.endswith('/'):
                    os.makedirs(install_root / p)
                else:
                    os.makedirs(os.path.dirname(install_root / p))
                    with open(install_root / p, 'wb'):
                        pass
            snapshot_dir = Path(load_location('rpm', 'repo-snapshot'))
            # Note: this can't use `_yum_using_build_appliance` because that
            # would lose coverage info on `yum_dnf_from_snapshot.py`.  On
            # the other hand, running this test against the host is fragile
            # since it depends on the system packages available on CI
            # containers.  For this reason, this entire test is an
            # `image.python_unittest` that runs in a build appliance.
            with tempfile.NamedTemporaryFile(mode='w') as tf:
                if version_lock:
                    tf.write('\n'.join(version_lock) + '\n')
                tf.flush()
                yum_dnf_from_snapshot(
                    yum_dnf=self._YUM_DNF,
                    repo_server_bin=Path(load_location('rpm', 'repo-server')),
                    storage_cfg=json.dumps({
                        'key': 'test',
                        'kind': 'filesystem',
                        'base_dir': (snapshot_dir / 'storage').decode(),
                    }),
                    snapshot_dir=snapshot_dir,
                    install_root=Path(install_root),
                    protected_paths=protected_paths,
                    versionlock_list=tf.name,
                    yum_dnf_args=_INSTALL_ARGS,
                )
            yield install_root
        finally:
            assert install_root != '/'
            # Courtesy of `yum`, the `install_root` is now owned by root.
            subprocess.run(['sudo', 'rm', '-rf', install_root], check=True) 
Example 16
Project: fs_image   Author: facebookincubator   File: filesystem_storage.py    MIT License 5 votes vote down vote up
def writer(self) -> ContextManager[StorageOutput]:
        sid = str(uuid.uuid4()).replace('-', '')
        sid_path = self._path_for_storage_id(sid)
        try:
            os.makedirs(sid_path.dirname())
        except FileExistsError:  # pragma: no cover
            pass

        with os.fdopen(os.open(
            sid_path,
            os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_CLOEXEC,
            mode=stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH,
        ), 'wb') as outfile:

            @contextmanager
            def get_id_and_release_resources():
                try:
                    yield sid
                finally:
                    # This `close()` flushes, making the written data readable,
                    # and prevents more writes via `StorageOutput`.
                    outfile.close()

            # `_CommitCallback` has a `try` to clean up on error. This
            # placement of the context assumes that `os.fdopen` cannot fail.
            with _CommitCallback(self, get_id_and_release_resources) as commit:
                yield StorageOutput(output=outfile, commit_callback=commit) 
Example 17
Project: fs_image   Author: facebookincubator   File: test_subvolume_garbage_collector.py    MIT License 5 votes vote down vote up
def test_gc_fails_when_wrapper_has_more_than_one(self):
        with tempfile.TemporaryDirectory() as refs_dir, \
             tempfile.TemporaryDirectory() as subs_dir:
            os.makedirs(os.path.join(subs_dir, 'no:refs/subvol1'))
            os.makedirs(os.path.join(subs_dir, 'no:refs/subvol2'))
            with self.assertRaisesRegex(
                RuntimeError, 'must contain only the subvol'
            ):
                sgc.subvolume_garbage_collector([
                    '--refcounts-dir', refs_dir,
                    '--subvolumes-dir', subs_dir,
                ]) 
Example 18
Project: fs_image   Author: facebookincubator   File: test_subvolume_on_disk.py    MIT License 5 votes vote down vote up
def test_from_subvolume_path(self):
        with tempfile.TemporaryDirectory() as td:
            # Note: Unlike test_from_serializable_dict_and_validation, this
            # test does NOT use a trailing / (to increase coverage).
            subvols = td.rstrip('/')
            rel_path = 'test_rule:vvv/test:subvol'
            subvol_path = os.path.join(subvols, rel_path)
            os.makedirs(subvol_path)  # `from_serializable_dict` checks this

            subvol = subvolume_on_disk.SubvolumeOnDisk.from_subvolume_path(
                subvol_path=subvol_path, subvolumes_dir=subvols,
            )
            with unittest.mock.patch('os.listdir') as listdir:
                listdir.return_value = ['test:subvol']
                self._check(
                    subvol,
                    subvol_path,
                    subvolume_on_disk.SubvolumeOnDisk(**{
                        subvolume_on_disk._BTRFS_UUID:
                            self._test_uuid(subvol_path),
                        subvolume_on_disk._BTRFS_PARENT_UUID: 'zupa',
                        subvolume_on_disk._HOSTNAME: _MY_HOST,
                        subvolume_on_disk._SUBVOLUME_REL_PATH: rel_path,
                        subvolume_on_disk._SUBVOLUMES_BASE_DIR: subvols,
                    }),
                )
                self.assertEqual(
                    listdir.call_args_list,
                    [((os.path.dirname(subvol_path),),)] * 2,
                )

            with self.assertRaisesRegex(
                RuntimeError, 'must be located inside the subvolumes directory'
            ):
                subvolume_on_disk.SubvolumeOnDisk.from_subvolume_path(
                    subvol_path=subvol_path, subvolumes_dir=subvols + '/bad',
                ) 
Example 19
Project: fs_image   Author: facebookincubator   File: common.py    MIT License 5 votes vote down vote up
def populate_temp_filesystem(img_path):
    'Matching Provides are generated by _temp_filesystem_provides'

    def p(img_rel_path):
        return os.path.join(img_path, img_rel_path)

    os.makedirs(p('a/b/c'))
    os.makedirs(p('a/d'))

    for filepath in ['a/E', 'a/d/F', 'a/b/c/G']:
        with open(p(filepath), 'w') as f:
            f.write('Hello, ' + filepath) 
Example 20
Project: leapp-repository   Author: oamg   File: actor.py    Apache License 2.0 5 votes vote down vote up
def generate_link_file(self, interface):

        try:
            os.makedirs('/etc/systemd/network')
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        link_file = '/etc/systemd/network/10-leapp-{}.link'.format(interface.name)
        with open(link_file, 'w') as f:
            f.write(LINK_FILE_TEMPLATE.format(interface.mac, interface.name))

        self.initrd_files.append(link_file) 
Example 21
Project: leapp-repository   Author: oamg   File: mounting.py    Apache License 2.0 5 votes vote down vote up
def _makedirs(path, mode=0o777, exists_ok=True):
    """ Helper function which extends os.makedirs with exists_ok on all versions of python. """
    try:
        os.makedirs(path, mode=mode)
    except OSError:
        if not exists_ok or not os.path.isdir(path):
            raise 
Example 22
Project: leapp-repository   Author: oamg   File: mounting.py    Apache License 2.0 5 votes vote down vote up
def makedirs(self, path, mode=0o777, exists_ok=True):
        """
        Creates the whole path recursively for any missing part.
        """
        _makedirs(path=self.full_path(path), mode=mode, exists_ok=exists_ok) 
Example 23
Project: Coulomb   Author: DynamoDS   File: extract_sessions_buffered.py    MIT License 5 votes vote down vote up
def flush(outpath, session_maps):

    lns_count = 0
    for sessions_lst in session_maps.values():
        lns_count += len(sessions_lst)

    log ("Flushing lines/sessions: " + str(lns_count) + " / " + str(len(session_maps.keys())))

    # Verify that the folders exist
    for session in session_maps.keys():
        sessions_folder_path = join(outPath, session[0:3])
        if sessions_folder_path not in existing_sessions_path:
            if not os.path.exists(sessions_folder_path):
                os.makedirs(sessions_folder_path)
            existing_sessions_path.add(sessions_folder_path)

        sessionPath = join(sessions_folder_path, session + ".gz")
        if not os.path.exists(sessionPath):
            newSessionIDSet.add(session)
            

        o = gzip.open(sessionPath, 'a')
        # o = open(join(outPath, session + ".json"), 'a')
        for ln in session_maps[session]:
            o.write(ln)
        o.flush()

        f = open(completedInputListPath, 'a')
        for filePath in completedInputFiles_buffer:
            completedInputFiles.add(filePath)
            f.write(filePath + "\n")
        f.flush()
        completedInputFiles_buffer.clear()

    log ("Flushing complete. Total sessions:\t" + str(len(sessionIDSet)) + "\tTotal new sessions:\t" + str(len(newSessionIDSet))) 
Example 24
Project: mlbv   Author: kmac   File: request.py    GNU General Public License v3.0 5 votes vote down vote up
def _get_cachedir():
    cachedir = os.path.join(util.get_tempdir(), 'cache')
    if not os.path.exists(cachedir):
        LOG.debug('Creating cache directory: ' + cachedir)
    if not os.path.exists(cachedir):
        os.makedirs(cachedir)
    return cachedir 
Example 25
Project: mlbv   Author: kmac   File: config.py    GNU General Public License v3.0 5 votes vote down vote up
def generate_config(username=None, password=None, servicename="MLB.tv"):
        """Creates config file from template + user prompts."""
        script_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
        # use the script name minus any extension for the config directory
        config_dir = None
        config_dir = os.path.join(Config.config_dir_roots[1], script_name)
        if not os.path.exists(config_dir):
            print("Creating config directory: {}".format(config_dir))
            os.makedirs(config_dir)
        config_file = os.path.join(config_dir, 'config')
        if os.path.exists(config_file):
            print("Aborting: The config file already exists at '{}'".format(config_file))
            return False

        # copy the template config file
        print("Generating basic config file at: {}".format(config_dir))
        current_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
        template_config_path = os.path.abspath(os.path.join(current_dir, '../../..', 'config.template'))
        if not os.path.exists(template_config_path):
            print("Could not find template config file [expected at: {}]".format(template_config_path))
            return False

        if username is None:
            username = input('Enter {} username: '.format(servicename))
        if password is None:
            password = input('Enter {} password: '.format(servicename))

        with open(template_config_path, 'r') as infile, open(config_file, 'w') as outfile:
            for line in infile:
                if line.startswith('# username='):
                    outfile.write("username={}\n".format(username))
                elif line.startswith('# password='):
                    outfile.write("password={}\n".format(password))
                else:
                    outfile.write(line)
        print("Finished creating config file: {}".format(config_file))
        print("You may want to edit it now to set up favourites, etc.")
        return True 
Example 26
Project: godot-mono-builds   Author: godotengine   File: os_utils.py    MIT License 5 votes vote down vote up
def mkdir_p(path):
    if not os.path.exists(path):
        print('creating directory: ' + path)
        os.makedirs(path)


# Remove files and/or directories recursively 
Example 27
Project: chainer-openai-transformer-lm   Author: soskek   File: utils.py    MIT License 5 votes vote down vote up
def make_path(f):
    d = os.path.dirname(f)
    if d and not os.path.exists(d):
        os.makedirs(d)
    return f 
Example 28
Project: chainer-openai-transformer-lm   Author: soskek   File: train.py    MIT License 5 votes vote down vote up
def predict():
    filename = filenames[dataset]
    pred_fn = pred_fns[dataset]
    label_decoder = label_decoders[dataset]
    predictions = pred_fn(iter_predict(teX, teM))
    if label_decoder is not None:
        predictions = [label_decoder[prediction] for prediction in predictions]
    path = os.path.join(submission_dir, filename)
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, 'w') as f:
        f.write('{}\t{}\n'.format('index', 'prediction'))
        for i, prediction in enumerate(predictions):
            f.write('{}\t{}\n'.format(i, prediction)) 
Example 29
Project: csv-to-calendar   Author: egeldenhuys   File: upload.py    GNU General Public License v3.0 5 votes vote down vote up
def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'calendar-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:  # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials 
Example 30
Project: malcode   Author: moonsea   File: zeustracker.py    GNU General Public License v3.0 5 votes vote down vote up
def GetFile(url, file):
    # url = ''.join(['http://vxheaven.org/dl/', file])

    if not os.path.exists('resource/zeustracker'):
        os.makedirs('resource/zeustracker')

    file_path = os.path.join('resource/zeustracker', file)

    print '[+] start downloading ', file
    urllib.urlretrieve(url, file_path) 
Example 31
Project: malcode   Author: moonsea   File: filetype.py    GNU General Public License v3.0 5 votes vote down vote up
def CopyFile(srcDir, desDir):
    if not os.path.exists(desDir):
        try:
            os.makedirs(desDir)
        except:
            print '[-] Mkdir error'

    print '[+]Copying ', srcDir

    try:
        shutil.copy(srcDir, desDir)
    except:
        print '[-]Copy error' 
Example 32
Project: malcode   Author: moonsea   File: filtersamemd5.py    GNU General Public License v3.0 5 votes vote down vote up
def checkDir(dir):
    if not os.path.exists(dir):
        try:
            os.makedirs(dir)
        except:
            print '[-] Mkdir error'
    print '[+]' + dir + ' is ok' 
Example 33
Project: malcode   Author: moonsea   File: vxheaven.py    GNU General Public License v3.0 5 votes vote down vote up
def GetFile(url, file):
    # url = ''.join(['http://vxheaven.org/dl/', file])

    if not os.path.exists('malcode/src'):
        os.makedirs('malcode/src')

    file_path = os.path.join('malcode', file)

    print '[+] start downloading ', file
    urllib.urlretrieve(url, file_path) 
Example 34
Project: malcode   Author: moonsea   File: wingenasm.py    GNU General Public License v3.0 5 votes vote down vote up
def log(action, content, prefix='[+]', suffix='', subpath=''):
    logDir = os.path.join(logPath, subpath)
    if not os.path.exists(logDir):
        try:
            os.makedirs(logDir)
        except:
            print '[-] Mkdir error'

    logpath = os.path.join(logDir, logName)

    with open(logpath, 'a+') as logfile:
        logfile.write(''.join([prefix, getNowTime(), ' ', action, ' ', content, suffix, '\n'])) 
Example 35
Project: malcode   Author: moonsea   File: getOpCode.py    GNU General Public License v3.0 5 votes vote down vote up
def checkDir(dirpath):
    if not os.path.exists(dirpath):
        try:
            os.makedirs(dirpath)
        except:
            print '[-] Mkdir error' 
Example 36
Project: malcode   Author: moonsea   File: virusgenasm.py    GNU General Public License v3.0 5 votes vote down vote up
def checkDir(dir):
    if not os.path.exists(dir):
        try:
            os.makedirs(dir)
        except:
            print '[-] Mkdir error'
    print '[+]' + dir + ' is ok' 
Example 37
Project: malcode   Author: moonsea   File: getOpCodeNdisasm.py    GNU General Public License v3.0 5 votes vote down vote up
def checkDir(dirpath):
    if not os.path.exists(dirpath):
        try:
            os.makedirs(dirpath)
        except:
            print '[-] Mkdir error' 
Example 38
Project: malcode   Author: moonsea   File: dosgenasm.py    GNU General Public License v3.0 5 votes vote down vote up
def log(action, content, prefix='[+]', suffix=''):
    if not os.path.exists(logPath):
        try:
            os.makedirs(logPath)
        except:
            print '[-] Mkdir error'

    log = ''.join([prefix, getNowTime(), ' ', action, ' ', content, suffix, '\n'])
    print log
	
    logpath = os.path.join(logPath, logName)

    with open(logpath, 'a+') as logfile:
        # logfile.write(''.join([prefix, getNowTime(), ' ', action, ' ', content, suffix, '\n']))
        logfile.write(log) 
Example 39
Project: malcode   Author: moonsea   File: benignasm.py    GNU General Public License v3.0 5 votes vote down vote up
def log(action, content, prefix='[+]', suffix='', subpath=''):
    logDir = os.path.join(logPath, subpath)
    if not os.path.exists(logDir):
        try:
            os.makedirs(logDir)
        except:
            print '[-] Mkdir error'

    logpath = os.path.join(logDir, logName)

    with open(logpath, 'a+') as logfile:
        logfile.write(''.join([prefix, getNowTime(), ' ', action, ' ', content, suffix, '\n'])) 
Example 40
Project: alfred-yubikey-otp   Author: robertoriv   File: workflow.py    MIT License 5 votes vote down vote up
def _create(self, dirpath):
        """Create directory `dirpath` if it doesn't exist.

        :param dirpath: path to directory
        :type dirpath: ``unicode``
        :returns: ``dirpath`` argument
        :rtype: ``unicode``

        """
        if not os.path.exists(dirpath):
            os.makedirs(dirpath)
        return dirpath 
Example 41
Project: pyblish-win   Author: pyblish   File: dist.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def bundle(src, dst):
    """Bundle files from `src` into `dst`

    Arguments:
        src (str): Source directory of files to bundle, e.g. /.
        dst (str): Output directory in which to copy files /build

    """

    print("Collecting files..")

    col = collect(src)

    print("Copying files into /build")
    for fname in col:
        out = os.path.join(dst, fname)

        try:
            os.makedirs(os.path.dirname(out))
        except WindowsError:
            pass

        shutil.copyfile(src=fname, dst=out)

    # Replace with a light-weight version
    shutil.copy(src=os.path.join(src, "icudt53.dll"),
                dst=os.path.join(dst, "lib", "python-qt5", "PyQt5"))

    print("Build finished successfully.")

    return dst 
Example 42
Project: pyblish-win   Author: pyblish   File: main.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def write_file(self, new_text, filename, old_text, encoding):
        orig_filename = filename
        if self._output_dir:
            if filename.startswith(self._input_base_dir):
                filename = os.path.join(self._output_dir,
                                        filename[len(self._input_base_dir):])
            else:
                raise ValueError('filename %s does not start with the '
                                 'input_base_dir %s' % (
                                         filename, self._input_base_dir))
        if self._append_suffix:
            filename += self._append_suffix
        if orig_filename != filename:
            output_dir = os.path.dirname(filename)
            if not os.path.isdir(output_dir):
                os.makedirs(output_dir)
            self.log_message('Writing converted %s to %s.', orig_filename,
                             filename)
        if not self.nobackups:
            # Make backup
            backup = filename + ".bak"
            if os.path.lexists(backup):
                try:
                    os.remove(backup)
                except os.error, err:
                    self.log_message("Can't remove backup %s", backup)
            try:
                os.rename(filename, backup)
            except os.error, err:
                self.log_message("Can't rename %s to %s", filename, backup)
        # Actually write the new file 
Example 43
Project: pyblish-win   Author: pyblish   File: install.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def create_home_path(self):
        """Create directories under ~
        """
        if not self.user:
            return
        home = convert_path(os.path.expanduser("~"))
        for name, path in self.config_vars.iteritems():
            if path.startswith(home) and not os.path.isdir(path):
                self.debug_print("os.makedirs('%s', 0700)" % path)
                os.makedirs(path, 0700)

    # -- Command execution methods ------------------------------------- 
Example 44
Project: pyblish-win   Author: pyblish   File: test_all.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def get_new_environment_path() :
    path=get_new_path("environment")
    import os
    try:
        os.makedirs(path,mode=0700)
    except os.error:
        test_support.rmtree(path)
        os.makedirs(path)
    return path 
Example 45
Project: pyblish-win   Author: pyblish   File: test_os.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_makedir(self):
        base = test_support.TESTFN
        path = os.path.join(base, 'dir1', 'dir2', 'dir3')
        os.makedirs(path)             # Should work
        path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4')
        os.makedirs(path)

        # Try paths with a '.' in them
        self.assertRaises(OSError, os.makedirs, os.curdir)
        path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir)
        os.makedirs(path)
        path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4',
                            'dir5', 'dir6')
        os.makedirs(path) 
Example 46
Project: pyblish-win   Author: pyblish   File: test_glob.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def mktemp(self, *parts):
        filename = self.norm(*parts)
        base, file = os.path.split(filename)
        if not os.path.exists(base):
            os.makedirs(base)
        f = open(filename, 'w')
        f.close() 
Example 47
Project: arm_now   Author: nongiach   File: download.py    MIT License 5 votes vote down vote up
def download_from_github(arch):
    templates = str(Path.home()) + "/.config/arm_now/templates/"
    os.makedirs(templates)
    filename = arch + ".tar.xz"
    URL = "https://github.com/nongiach/arm_now_templates/raw/master/"
    download(URL + filename, templates + filename, Config.DOWNLOAD_CACHE_DIR) 
Example 48
Project: arm_now   Author: nongiach   File: arm_now.py    MIT License 5 votes vote down vote up
def do_offline():
    URL = "https://github.com/nongiach/arm_now_templates/archive/master.zip"
    templates = str(Path.home()) + "/.config/arm_now/templates/"
    master_zip = str(Path.home()) + "/.config/arm_now/templates/master.zip"
    os.makedirs(templates)
    # download_from_github(arch)
    download(URL, master_zip, Config.DOWNLOAD_CACHE_DIR)
    os.chdir(templates)
    check_call("unzip master.zip", shell=True)
    check_call("mv arm_now_templates-master/* .", shell=True)
    check_call("rm -rf arm_now_templates-master/ README.md master.zip", shell=True) 
Example 49
Project: robust-atd   Author: shadowbq   File: __init__.py    MIT License 5 votes vote down vote up
def mkdir_p(self, path):
        try:
            os.makedirs(path)
            if self.options.verbosity:
                print ('mkdir_p %s' % path)
                sys.stdout.flush()
        except OSError as exc:  # Python >2.5
            if exc.errno == errno.EEXIST and os.path.isdir(path):
                pass
            else:
                raise 
Example 50
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: workflow.py    MIT License 5 votes vote down vote up
def _create(self, dirpath):
        """Create directory `dirpath` if it doesn't exist.

        :param dirpath: path to directory
        :type dirpath: ``unicode``
        :returns: ``dirpath`` argument
        :rtype: ``unicode``

        """
        if not os.path.exists(dirpath):
            os.makedirs(dirpath)
        return dirpath