<commit_before>import os
import re
import sys
import shutil


def update_metatlas(directory):

    new_files = []
    for root, directories, filenames in os.walk(directory):
        for fname in filenames:
            if fname.endswith('.mzML'):
                fname = os.path.join(root, fname)
                hdf5_file = fname.replace('.mzML', '.h5')
                if not os.path.exists(hdf5_file):
                    new_files.append(fname)

    patt = re.compile(r".+\/raw_data\/(?P<username>[^/]+)\/(?P<experiment>[^/]+)\/(?P<path>.+)")

    print('Found %s new files' % len(new_files))

    for fname in new_files:
        info = patt.match(os.path.abspath(fname))
        if info:
            info = info.groupdict()
        else:
            print("Invalid path name", fname)
            continue

        print(fname)
        try:
            os.chmod(fname, 0o660)
        except OSError:
            pass

        # copy the original file to a pasteur backup
        if os.environ['USER'] == 'pasteur':
            pasteur_path = fname.replace('raw_data', 'pasteur_backup')
            dname = os.path.dirname(pasteur_path)
            if not os.path.exists(dname):
                os.makedirs(dname)
            shutil.copy(fname, pasteur_path)

        # todo: search the database at the end for unreachable files

        # convert to HDF and store the entry in the database
        try:
            from metatlas import LcmsRun, mzml_to_hdf
            hdf5_file = mzml_to_hdf(fname)
            os.chmod(hdf5_file, 0o660)
            description = info['experiment'] + ' ' + info['path']
            ctime = os.stat(fname).st_ctime
            run = LcmsRun(name=info['path'], description=description,
                          created_by=info['username'],
                          modified_by=info['username'],
                          created=ctime, last_modified=ctime,
                          mzml_file=fname, hdf5_file=hdf5_file)
            run.store()
        except Exception as e:
            print(e)


if __name__ == '__main__':<commit_msg>Better handling of chmod error<commit_after>import os
import re
import sys
import shutil


def update_metatlas(directory):

    new_files = []
    for root, directories, filenames in os.walk(directory):
        for fname in filenames:
            if fname.endswith('.mzML'):
                fname = os.path.join(root, fname)
                hdf5_file = fname.replace('.mzML', '.h5')
                if not os.path.exists(hdf5_file):
                    new_files.append(fname)

    patt = re.compile(r".+\/raw_data\/(?P<username>[^/]+)\/(?P<experiment>[^/]+)\/(?P<path>.+)")

    print('Found %s new files' % len(new_files))

    for fname in new_files:
        info = patt.match(os.path.abspath(fname))
        if info:
            info = info.groupdict()
        else:
            print("Invalid path name", fname)
            continue

        print(fname)
        try:
            os.chmod(fname, 0o660)
        except Exception as e:
            print(e)

        # copy the original file to a pasteur backup
        if os.environ['USER'] == 'pasteur':
            pasteur_path = fname.replace('raw_data', 'pasteur_backup')
            dname = os.path.dirname(pasteur_path)
            if not os.path.exists(dname):
                os.makedirs(dname)
            shutil.copy(fname, pasteur_path)

        # todo: search the database at the end for unreachable files

        # convert to HDF and store the entry in the database
        try:
            from metatlas import LcmsRun, mzml_to_hdf
            hdf5_file = mzml_to_hdf(fname)
            os.chmod(hdf5_file, 0o660)
            description = info['experiment'] + ' ' + info['path']
            ctime = os.stat(fname).st_ctime
            run = LcmsRun(name=info['path'], description=description,
                          created_by=info['username'],
                          modified_by=info['username'],
                          created=ctime, last_modified=ctime,
                          mzml_file=fname, hdf5_file=hdf5_file)
            run.store()
        except Exception as e:
            print(e)


if __name__ == '__main__':
{'fraction_non_alphanumeric': 0.11297271421732887, 'fraction_numerical': 0.007180469123982767, 'mean_word_length': 1.8065815983881799, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2619899', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1286, 'n_words': 281}