<commit_before>#!/usr/bin/python2
import os
import re
import errno
import ConfigParser

from time import sleep

def remove_prefix(text, prefix):
    if text.startswith(prefix):
        return text[len(prefix):]
    return text  # or whatever

def create_path(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

def remove_path(path):
    try:
        os.removedirs(path)
    except OSError as exception:
        if exception.errno != errno.ENOTEMPTY:
            raise

class Config():
    def __init__(self):
        self.config = ConfigParser.SafeConfigParser()
        self.config.read("./config.ini")
        self.source_dir = self.config.get("settings", "source_directory")
        self.target_dir = self.config.get("settings", "target_directory")

class Show():
    def __init__(self, filename, path):
        self.file = filename
        self.path = path
        self.generate_metadata()

    def generate_metadata(self):
        res = re.search("^(.+)\.S(\d+)E([E\d-]+)\..+$", self.file)
        if res is not None:
            self.name = res.group(1)
            self.season = res.group(2)
            self.episode = res.group(3)

    def destination(self):
        """
        Returns the full directory structure of where
        the symlink for the show should go
        """
        config = Config()
        return "{}/{}/Season.{}".format(config.target_dir, self.name, self.season)

    def create_link(self):
        if not hasattr(self, 'name'):
            print "Warning, failed for %s/%s" % (self.path, self.file)
            return
        create_path(self.destination())
        try:
            os.symlink("{}/{}".format(self.path, self.file), "{}/{}".format(self.destination(), self.file))
        except OSError as exception:
            if exception.errno != errno.EEXIST:
                raise

def main():
    config = Config()
    while True:
        for root, dirs, files in os.walk(config.source_dir):
            for f in files:
                show = Show(f, root)
                show.create_link()
        for root, dirs, files in os.walk(config.target_dir):
            for f in files:
                try:
                    os.stat("{}/{}".format(root, f))
                except OSError as exception:
                    if exception.errno != errno.ENOENT:
                        raise
                    else:
                        os.remove("{}/{}".format(root, f))
        sleep(60)

if __name__=="__main__":
    main()
<commit_msg>No longer runs infinitely<commit_after>#!/usr/bin/python2
import os
import re
import errno
import ConfigParser

from time import sleep

def remove_prefix(text, prefix):
    if text.startswith(prefix):
        return text[len(prefix):]
    return text  # or whatever

def create_path(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

def remove_path(path):
    try:
        os.removedirs(path)
    except OSError as exception:
        if exception.errno != errno.ENOTEMPTY:
            raise

class Config():
    def __init__(self):
        self.config = ConfigParser.SafeConfigParser()
        self.config.read("./config.ini")
        self.source_dir = self.config.get("settings", "source_directory")
        self.target_dir = self.config.get("settings", "target_directory")

class Show():
    def __init__(self, filename, path):
        self.file = filename
        self.path = path
        self.generate_metadata()

    def generate_metadata(self):
        res = re.search("^(.+)\.S(\d+)E([E\d-]+)\..+$", self.file)
        if res is not None:
            self.name = res.group(1)
            self.season = res.group(2)
            self.episode = res.group(3)

    def destination(self):
        """
        Returns the full directory structure of where
        the symlink for the show should go
        """
        config = Config()
        return "{}/{}/Season.{}".format(config.target_dir, self.name, self.season)

    def create_link(self):
        if not hasattr(self, 'name'):
            print "Warning, failed for %s/%s" % (self.path, self.file)
            return
        create_path(self.destination())
        try:
            os.symlink("{}/{}".format(self.path, self.file), "{}/{}".format(self.destination(), self.file))
        except OSError as exception:
            if exception.errno != errno.EEXIST:
                raise

def main():
    config = Config()

    # Try to create symlinks
    for root, dirs, files in os.walk(config.source_dir):
        for f in files:
            show = Show(f, root)
            show.create_link()

    # Remove dead links
    for root, dirs, files in os.walk(config.target_dir):
        for f in files:
            try:
                os.stat("{}/{}".format(root, f))
            except OSError as exception:
                if exception.errno != errno.ENOENT:
                    raise
                else:
                    os.remove("{}/{}".format(root, f))

if __name__=="__main__":
    main()
{'fraction_non_alphanumeric': 0.1273474178403756, 'fraction_numerical': 0.0019561815336463224, 'mean_word_length': 1.953783939919122, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '5402614', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1484, 'n_words': 379}