<commit_before>#!/usr/bin/python
import os
import sys
from stat import *
import zmq
import threading
import time
import math
import socket
import signal
import argparse
from urlparse import urlparse
import shutil
import subprocess

import ConfigParser
import linuxcnc
import preview
from machinekit import service
from machinekit import config

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

from message_pb2 import Container
from config_pb2 import *
from types_pb2 import *
from status_pb2 import *
from preview_pb2 import *
from motcmds_pb2 import *
from object_pb2 import ProtocolParameters


def printError(msg):
    sys.stderr.write('ERROR: ' + msg + '\n')


def getFreePort():
    sock = socket.socket()
    sock.bind(('', 0))
    port = sock.getsockname()[1]
    sock.close()
    return port


class CustomFTPHandler(FTPHandler):

    def on_file_received(self, file):
        # do something when a file has been received
        pass  # TODO inform client about new file

    def on_incomplete_file_received(self, file):
        # remove partially uploaded files
        os.remove(file)


class FileService(threading.Thread):

    def __init__(self, iniFile=None, host='', svcUuid=None,
                loopback=False, debug=False):
        self.debug = debug
        self.host = host
        self.loopback = loopback
        self.shutdown = threading.Event()
        self.running = False

        # Linuxcnc
        try:
            iniFile = iniFile or os.environ.get('INI_FILE_NAME', '/dev/null')
            self.ini = linuxcnc.ini(iniFile)
            self.directory = self.ini.find('DISPLAY', 'PROGRAM_PREFIX') or os.getcwd()
            self.directory = os.path.expanduser(self.directory)
        except linuxcnc.error as detail:
            printError(str(detail))
            sys.exit(1)

        self.filePort = getFreePort()
        self.fileDsname = "ftp://" + self.host + ":" + str(self.filePort)

        self.fileService = service.Service(type='file',
                                   svcUuid=svcUuid,
                                   dsn=self.fileDsname,
                                   port=self.filePort,
                                   host=self.host,
                                   loopback=self.loopback,
                                   debug=self.debug)

        #FTP
        # Instantiate a dummy authorizer for managing 'virtual' users
        self.authorizer = DummyAuthorizer()

        # anonymous user has full read write access
        self.authorizer.add_anonymous(self.directory, perm="lradw")

        # Instantiate FTP handler class
        self.handler = CustomFTPHandler
        self.handler.authorizer = self.authorizer

        # Define a customized banner (string returned when client connects)
        self.handler.banner = "welcome to the GCode file service"
<commit_msg>machinetalk/mkwrapper: add cd and mkdir permission to FTP server<commit_after>#!/usr/bin/python
import os
import sys
from stat import *
import zmq
import threading
import time
import math
import socket
import signal
import argparse
from urlparse import urlparse
import shutil
import subprocess

import ConfigParser
import linuxcnc
import preview
from machinekit import service
from machinekit import config

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

from message_pb2 import Container
from config_pb2 import *
from types_pb2 import *
from status_pb2 import *
from preview_pb2 import *
from motcmds_pb2 import *
from object_pb2 import ProtocolParameters


def printError(msg):
    sys.stderr.write('ERROR: ' + msg + '\n')


def getFreePort():
    sock = socket.socket()
    sock.bind(('', 0))
    port = sock.getsockname()[1]
    sock.close()
    return port


class CustomFTPHandler(FTPHandler):

    def on_file_received(self, file):
        # do something when a file has been received
        pass  # TODO inform client about new file

    def on_incomplete_file_received(self, file):
        # remove partially uploaded files
        os.remove(file)


class FileService(threading.Thread):

    def __init__(self, iniFile=None, host='', svcUuid=None,
                loopback=False, debug=False):
        self.debug = debug
        self.host = host
        self.loopback = loopback
        self.shutdown = threading.Event()
        self.running = False

        # Linuxcnc
        try:
            iniFile = iniFile or os.environ.get('INI_FILE_NAME', '/dev/null')
            self.ini = linuxcnc.ini(iniFile)
            self.directory = self.ini.find('DISPLAY', 'PROGRAM_PREFIX') or os.getcwd()
            self.directory = os.path.expanduser(self.directory)
        except linuxcnc.error as detail:
            printError(str(detail))
            sys.exit(1)

        self.filePort = getFreePort()
        self.fileDsname = "ftp://" + self.host + ":" + str(self.filePort)

        self.fileService = service.Service(type='file',
                                   svcUuid=svcUuid,
                                   dsn=self.fileDsname,
                                   port=self.filePort,
                                   host=self.host,
                                   loopback=self.loopback,
                                   debug=self.debug)

        #FTP
        # Instantiate a dummy authorizer for managing 'virtual' users
        self.authorizer = DummyAuthorizer()

        # anonymous user has full read write access
        self.authorizer.add_anonymous(self.directory, perm="lredwm")

        # Instantiate FTP handler class
        self.handler = CustomFTPHandler
        self.handler.authorizer = self.authorizer

        # Define a customized banner (string returned when client connects)
        self.handler.banner = "welcome to the GCode file service"

{'fraction_non_alphanumeric': 0.07937584803256445, 'fraction_numerical': 0.0033921302578018998, 'mean_word_length': 2.3429705215419503, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2874098', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1615, 'n_words': 452}