MadelineProto/libpy2php/os_path.php

252 lines
5.2 KiB
PHP
Raw Normal View History

2016-06-23 23:51:08 +02:00
<?php
2016-07-14 15:15:50 +02:00
require_once dirname(__FILE__).'/os.php';
2016-06-23 23:51:08 +02:00
/**
2016-07-14 15:15:50 +02:00
* A class to emulate python's os.path.
2016-06-23 23:51:08 +02:00
*/
2016-07-14 15:15:50 +02:00
class os_path
{
2016-06-23 23:51:08 +02:00
const supports_unicode_filenames = true;
2016-07-14 15:15:50 +02:00
public static function abspath($path)
{
2016-06-23 23:51:08 +02:00
return self::normpath(self::join(getcwd(), $path));
}
2016-07-14 15:15:50 +02:00
public static function basename($path)
{
2016-06-23 23:51:08 +02:00
// is this right?
return basename($path);
}
2016-07-14 15:15:50 +02:00
public static function commonprefix($list)
{
2016-06-23 23:51:08 +02:00
$pl = 0; // common prefix length
$n = count($list);
$l = strlen($list[0]);
while ($pl < $l) {
$c = $list[0][$pl];
2016-07-14 15:15:50 +02:00
for ($i = 1; $i < $n; $i++) {
2016-06-23 23:51:08 +02:00
if ($list[$i][$pl] !== $c) {
break 2;
}
}
$pl++;
}
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return substr($list[0], 0, $pl);
}
2016-07-14 15:15:50 +02:00
public static function dirname($path)
{
2016-06-23 23:51:08 +02:00
return dirname($path);
}
2016-07-14 15:15:50 +02:00
public static function exists($path)
{
2016-06-23 23:51:08 +02:00
return file_exists($path);
}
2016-07-14 15:15:50 +02:00
public static function lexists($path)
{
2016-06-23 23:51:08 +02:00
$rc = file_exists($path);
2016-07-14 15:15:50 +02:00
if (!$rc && is_link($path)) {
2016-06-23 23:51:08 +02:00
return true;
}
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $rc;
}
2016-07-14 15:15:50 +02:00
public static function expanduser($path)
{
if (strpos($path, '~') !== false) {
2016-06-23 23:51:08 +02:00
$info = posix_getpwuid(posix_getuid());
$path = str_replace('~', $info['dir'], $path);
}
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $path;
}
2016-07-14 15:15:50 +02:00
public static function expandvars($path)
{
2016-06-23 23:51:08 +02:00
$env = count($_ENV) ?: $_SERVER;
2016-07-14 15:15:50 +02:00
$map = [];
foreach ($env as $k => $v) {
if (!is_scalar($v)) {
2016-06-23 23:51:08 +02:00
continue;
}
2016-07-14 15:15:50 +02:00
$map['$'.$k] = $v;
$map['${'.$k.'}'] = $v;
2016-06-23 23:51:08 +02:00
}
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return strtr($path, $map);
}
2016-07-14 15:15:50 +02:00
public static function getatime($path)
{
2016-06-23 23:51:08 +02:00
try {
$rc = fileatime($path);
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $rc;
2016-07-14 15:15:50 +02:00
} catch (Exception $e) {
2016-06-23 23:51:08 +02:00
throw new OSError($e->getMessage, $e->getCode());
}
}
2016-07-14 15:15:50 +02:00
public static function getmtime($path)
{
2016-06-23 23:51:08 +02:00
try {
$rc = filemtime($path);
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $rc;
2016-07-14 15:15:50 +02:00
} catch (Exception $e) {
2016-06-23 23:51:08 +02:00
throw new OSError($e->getMessage, $e->getCode());
}
}
2016-07-14 15:15:50 +02:00
public static function getctime($path)
{
2016-06-23 23:51:08 +02:00
try {
$rc = filectime($path);
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $rc;
2016-07-14 15:15:50 +02:00
} catch (Exception $e) {
2016-06-23 23:51:08 +02:00
throw new OSError($e->getMessage, $e->getCode());
}
}
2016-07-14 15:15:50 +02:00
public static function getsize($path)
{
2016-06-23 23:51:08 +02:00
try {
$rc = filesize($path);
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $rc;
2016-07-14 15:15:50 +02:00
} catch (Exception $e) {
2016-06-23 23:51:08 +02:00
throw new OSError($e->getMessage, $e->getCode());
}
}
2016-07-14 15:15:50 +02:00
public static function isabs($path)
{
2016-06-23 23:51:08 +02:00
// fixme: implement check for windows.
return $path[0] == '/';
}
2016-07-14 15:15:50 +02:00
public static function isfile($path)
{
2016-06-23 23:51:08 +02:00
return is_file($path);
}
2016-07-14 15:15:50 +02:00
public static function isdir($path)
{
2016-06-23 23:51:08 +02:00
return is_dir($path);
}
2016-07-14 15:15:50 +02:00
public static function islink($path)
{
2016-06-23 23:51:08 +02:00
return is_link($path);
}
2016-07-14 15:15:50 +02:00
public static function ismount($path)
{
2016-06-23 23:51:08 +02:00
self::_unimplemented();
}
2016-07-14 15:15:50 +02:00
public static function split($path)
{
2016-06-23 23:51:08 +02:00
$parts = explode(DIRECTORY_SEPARATOR, $path);
2016-07-14 15:15:50 +02:00
$first = implode(DIRECTORY_SEPARATOR, array_slice($parts, 0, count($parts) - 1));
$last = $parts[count($parts) - 1];
return [$first, $last];
2016-06-23 23:51:08 +02:00
}
2016-07-14 15:15:50 +02:00
public static function join($path, ...$paths)
{
2016-06-23 23:51:08 +02:00
$buf = rtrim($path, '/');
2016-07-14 15:15:50 +02:00
foreach ($paths as $p) {
2016-06-23 23:51:08 +02:00
$i = 0;
2016-07-14 15:15:50 +02:00
$p = trim($p, '/');
$buf .= DIRECTORY_SEPARATOR.$p;
2016-06-23 23:51:08 +02:00
}
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $buf;
}
2016-07-14 15:15:50 +02:00
public static function normcase($path)
{
2016-06-23 23:51:08 +02:00
// fixme: different behavior on windows.
return $path;
}
2016-07-14 15:15:50 +02:00
public static function normpath($path)
{
2016-06-23 23:51:08 +02:00
return realpath($path);
}
2016-07-14 15:15:50 +02:00
public static function realpath($path)
{
2016-06-23 23:51:08 +02:00
return realpath($path);
}
2016-07-14 15:15:50 +02:00
public static function relpath($path, $start)
{
2016-06-23 23:51:08 +02:00
self::_unimplemented();
}
2016-07-14 15:15:50 +02:00
public static function samefile($path1, $path2)
{
2016-06-23 23:51:08 +02:00
return fileinode($path1) == fileinode($path2);
}
2016-07-14 15:15:50 +02:00
public static function sameopenfile($fd1, $fd2)
{
$s1 = fstat($fd1);
$s2 = fstat($fd2);
2016-06-23 23:51:08 +02:00
return $s1['ino'] == $s2['ino'];
}
2016-07-14 15:15:50 +02:00
public static function samestat($stat1, $stat2)
{
2016-06-23 23:51:08 +02:00
return $stat1 == $stat2;
}
2016-07-14 15:15:50 +02:00
public static function splitdrive($path)
{
2016-06-23 23:51:08 +02:00
//fixme: implement windows case.
2016-07-14 15:15:50 +02:00
return ['', $path];
2016-06-23 23:51:08 +02:00
}
2016-07-14 15:15:50 +02:00
public static function splitext($path)
{
2016-06-23 23:51:08 +02:00
$first = $path;
$second = '';
2016-07-14 15:15:50 +02:00
$pos = strrpos($path, '.');
if ($pos !== false) {
2016-06-23 23:51:08 +02:00
$first = substr($path, 0, $pos);
$second = substr($path, $pos);
}
2016-07-14 15:15:50 +02:00
return [$first, $second];
2016-06-23 23:51:08 +02:00
}
2016-07-14 15:15:50 +02:00
public static function splitunc($path)
{
2016-06-23 23:51:08 +02:00
self::_unimplemented();
}
2016-07-14 15:15:50 +02:00
public static function walk($path, $visit, $arg)
{
2016-06-23 23:51:08 +02:00
// Note: deprecated in python 3 in favor of os.walk()
self::_unimplemented();
}
2016-07-14 15:15:50 +02:00
private static function _unimplemented()
{
throw new Exception('Unimplemented. Please consider submitting a patch to py2php project on github.');
2016-06-23 23:51:08 +02:00
}
2016-07-14 15:15:50 +02:00
}